major rewrite, this will get ugly