Minor refactoring

* Extract a function
* Add explicit type signature to a function
* Use liftA2 to make some functions a bit more terse