Fix several bugs and code style issues.

Thanks to Simon Bischof for remarks that resulted in this commit,
improving the lexer.