Adding Connection class and some optimization