Fix directory name bug
Fix exit code and error logging
Refactor to support -e and -r options