Refactored and added tests/functionality