Initial working code

Needs code review.