Following kvales post, I'd like to add that you should write a bunch of tests to ensure that the code you produce works the same way as the original code. It's a hell of its own if you generate a sideeffect you haven't considered before and you'll notice it (too) late.