Create initial version

Seems to work as-is but needs testing and tuning.