simplify async code and fix leak on error