Cut down unnecessary sql calls and made code easier to read