big rewrite to make testing easier