Re-written from scratch, to update all the guts.