big refactor for better error reporting
improved error reporting