It took me WAY too long to get this logic right...Luckily the unit tests were great at verifying all these cases.