I have just read this very good article of Michael Schwern about Refactoring
The only problem I see is that usually code that looks like what he had in the example won't come with a test suit. So before you start refactoring you have to write that, but it is quite hard to write a test suit for something that you don't know what it is supposed to do.
I am looking forward to read the next article about how to write test suit to such code.