code refactored, all tests passed