overhauled diff implementation, tidied code