refactoring and adding sort options