Refactor and Adding some comments.

Refactored the dir check into a function, moved some other code around
and added some explanatory comments.