Added error handling

I added some error handling to make sure that the correct argument types
were being supplied, and that all the sort parameters supplied are
valid. I would remove this if I were sure my data and arguments were
correct, for performance reasons.