end of a weekend, a lot of new code and it actually works