a lot of fixes, finally a working version now