List files and directories separately

This finally fixes the issue where trying to list a file causes a crash. Also, tidy up some of the uses of references.