Refactored IP retrieval for better code reuse.