Updated version with sorting script