Rewrote everything.

Cleaner code; not yet ready for use - committed to try get help
debugging.