Refactor and improve lexer efficiency