Fixed some bugs with the list_dirs function, also separated out some common utilitys into a single file