refactored script so it query for only one template