Refactor code for clarity

- Added more comments
- Broke out inner loop through files
- Simplified some ifs with match
- Fixed line lengths