split sorting into functions