Initial working version. Cleaned up for publishing.