added some error handling and updated tests accordingly.