a lot of debugging, it now basically works