removed some spaghetti, but I have to debug a lot