Refactor code ready for changing database backend.