refactored out database connect into a function