trigger_error replaced with Exceptions
Fixed double rendering in case of errors