Fixed some crashes and other bugs