Rewrite of parser logic to easier deal with state changes