Fixed a crash and mem leak