Not working code commented. @Jan Dusek will fix it ASAP, as it is part of his bachelor thesis ;)