partial refactor to handle file IO better