Refactor to use Template Haskell