Refactoring: split out chunks of code from a huge method