Moved funtion to util and refactored/minor changes
Exception optimization
Formatting, removing trailing spaces/tabs
Workaround for off-by-one bug and fix issue that would lead to the
target directory being deleted if it existed.