added directory pre-checks, code cleanup