Various cleanups and minor bug fixes