Checking for result size before sorting. Added code to run on main thread.