Cleaned the code quite a bit, needs some bugfixing