New version, complete rewrite