nearly there, still a few obvious bugs