Fix a bug in the diff algorithm
Cleanup the code, re-organize