Updated Oppg4 with sorted optimization