Refactored and added more tests