Refactored more code, fixed error reporting
Refactored more code, fixed error reporting