Refactoring and added testing.