More work on #12

Previous implementation 015e290ea5ee44d88e451cbefe9a58608f3ef874 was broken. This now accurately handles my test cases. Next step is to write automated versions for #35