Fixed most of the bugs

Must check if everything is fine now.