refactored to pass the unit tests