Some crash fixes and error handling