just about a complete rewrite