Combine keyValid and clearKeys. Now uses single db connection with single commit.