Just set defaultgw after all gateways are processed, it fixes return_gateways_array()