Refactored out getconnected function