Fixed some crashes and failed unittests