improve transaction and error handling