Check for errors and clean up if encountered