Fix mod_dirs ordering and remove duplicate code