Refactored function and added tests.