refactored; tests written