use a context manager to take care of closing connections
refactor