Fixed first error, a segmentation fault caused by accessing null.
Fixed second error, a variable being accessed after being deallocated.
Still not able to remove garbage value. Useless commit.