refactored some functions and tests