General cleanup. Adds check for empty dirs.