merged and added some algorithm to detect location