New version, implemented from scratch.