cleanup of core code without changing the interface

Hope you don't mind! We're using this in gterrono/houston#255 and figured if we're using it, we should use your code rather than forking and maintaining our own.  This commit doesn't change the public-facing API, test[s] pass.