Added logic to do sorting