- simplify some ifs to whens
- remove unused definitions
- add some comments for sanity