basically a complete rewrite from build 23