work in progress.
First fully working version.