Fixed a lot of bugs and removed unused and wrong code lines.