Added input and sorting functions