Improved getting a valid hostname