move areSame helper into external file
fix potential integer overflow