Refactored some code, fixed test case output