Fix bug in recursive database search