Various bug fixes

/cc @ritch