Iterative Parser

Changed the parser from a recursive implementation to an iterative one. Also some other minor code cleanups.