Refactor code for file list creation

A lot of code in this area was duplicated, so better to remove that.