updated comparison algorithm to fix some bugs