Some cleanups and error checking