Initial code commit

This is the existing v1 code I wrote previously, with minor cleanup for
submission here.