fixed bug
Tried to fix bug. Should be completely rewritten...