Refactor transformer code

* Use more specific data type for simpler API
* Extract common patterns to make it easier to create new transformers