Refactored

Refactored and added tests