Fixed a lot of bugs, needs more fixing.