Fixed incompatibility issue with python 3 and add break to the loop to speed up the search