adjusted algorithm for max constrast