fix bugs and add callback for error cases