Added more error checking, and better handling of empty vectors