Changed GOTO into smarter if statements