refactored and sorted