Some Refactoring

Breaking out code to make it easerier to test