many bugfixes.
This is the fist working version.