refactoring selection handling code and directives