Implemented connection hooks, cleaned up some code