Guard against some nil errors in the sort_order code for images