Rewriting parser

Decided to merge in these changes, I think they are valuable at least to some degree.