Refactors some file work and improves unit test