Some pretty decent refactoring