Refactoring code to include a class