Refactored creation of directories code