More bug fixes

Last version failed in several respects when uploaded to AWS sandbox
installation. Obvious bugs have been mostly fixed. Changed sql to
retrieve active element IDs, and fixed a subtle problem applying
selection rules to items.