Tidy up db connection so it's a bit more reusable