First go at refactoring