Merged contrast function into main loop