Changed manually handling of errors to exceptions