refactoring in readPixels method