Use sorting to make sure un-needed resource applies don't occur. Fixes #43