Rearranged connection to db and added logging