Error reporting

Added more error reporting
Fixed code structure