general cleanup and error logging