clean up some code, use gotos