moved diff code into separate function.