Fix isDefined handling on references
Restructured the lexer once again (hopefully for the last time)