Added error handler for debug mode
Fixed bugs