Fix a few issues. Note that this is currently untested and undocumented.