debugged factored out databse connection