updated code as function for ArcGIS tool