Initial code

Is functional but far from bulletproof.