Fix and simplify the code for converting variables between types