fix not being able to go back after error

and tidy up some code.