Naive query rewriting
rewrite code structure to use typeclasses