Improved parser implementation and some general refactoring and cleanup