a bit of a rewrite