Improve generic typing and remove casts.
Make instance variables private and final where appropriate.
Fix bug - final entity type wasn't printed if last token of file is entity.
Tiny clean ups.