detect errors & cleanup better