Better error handling, cleanup