Admittedly this algorithm is clearly pretty hackish, and is possibly broken under certain circumstances. It would be good to add more robust test cases