some refactoring in kdb/(w)database.c