Refactoring the whole thing