Refactoring a lot of code