More correct calculation on quantized pixel
Fixing some memory leak