added automatic append of slash to dir and refactored