Fixed a lot of bugs and crappy code