Cleanup and extra error checking