Corrected the logic for discarding out of range pixels