Did some useful refactoring