Various fixes/enhancements

Broken into three commits now (as suggested), and the tests pass.
Good call. I've been working in functional programming land too long now, so my mind defaults to things like map and reduce.