Refactor to avoid regression
Add some tests to demonstrate why commit f4c4603 didn't quite fix the problem