Added optional callback to handle errors