parser: refactor main loop a bit