working on a neater rewrite