Refactor GLCM code for efficiency.