refactored connection management, added more documentation