fixed a lot of bugs and better error handler