Moved sorting into separate methods.