Extract most code into methods