Fixed many bugs, probably introduced some more