database refinement code