Appears to do what is required, may need some better test cases