final version with message to handle errors