Some cleanups and bug fixes