Complete re-write from scratch.