Refactored diff function.