Cleanup and error checking.