Code cleanup, and a few extra utility functions.