refactored and reduced code, modified tests