fixed a lot of bugs in the previous commit (doesn't crash anymore, but results are still wrong ...)