Refactored wifi connection function.