fixed dead code
fixed bad integer conversion
fixed NULL dereferencing