Refactor code to treat files more properly