Finished, works for all test cases