Switching from bit-array to database query (partially done)