adding protect_all_instances option
refactored security queries for improved performance