Revert "Revert "some cleanups on error funct""

This reverts commit e74593e3fd21bd2a9d17b0dbc0d0ffd2bd25b448.