fix race condition, error message, cleanup