Updated to handle errors correctly.