Moved raw data sorting to its own function.