Some cleanup and error catching