Refactored ping script