bad pointer fixes + optimization