added ping handling and re-organized logic
added ping response logic and re-organized logic