moving sql connection out
adding logging