Fixed some bugs and cleaned a bit.