so close to a template.  Every time I fix one bug I discover a new one, but I think I'm very close