First code

Needs more testing.