Modified code for multi-level selection