Refactoring a bunch of code