Added error handling on prepare method