refactored code and added tests for exposed functions