We could use some unit tests for these functions: they are a little tricky, and easy to unit test.