Completed code, passes all test cases