Refactor database operation