A mostly working version. Bed time...