See https://gist.github.com/joefiorini/3a8c36bcedf7ad954952 for an explanation of the comparison logic.