sorting and removing unnecessary variables