refactor database communications