Better handle of checking root and cleanup code