moved conversion code to lexer and fixed a few bugs