refactor pingHosts function