remove goto statements and small changes to code flow