Moved the grayscale conversion to a separate function.