Fixed some bugs that cause crashes