First working version

first working version  - just needs commentation and polish!