Refactor lexer implementation