improve gateway detection