refactored the connection stuff