Refactoring, and added tests back in.