Memory leak fix and possible null exception