Optimize the localization function