 	}
 	
 	/// The library XMLElement in the FCPXML document.
-	public var fcpxLibrary: XMLElement {
-		get {
-			let rootElement = self.child(at: 0) as! XMLElement
+	public var fcpxLibrary: XMLElement? {
+		get {
+			guard let rootElement = self.rootElement() else {
+				return nil
+			}
 			return rootElement.elements(forName: "library")[0]
 		}
 	}

			
			return nil
		}
	}
	
	/// The names of all events as a String array.

	
	// MARK: - Modification Functions
	
	/// Adds a resource XMLElement to the FCPXML document.
	///
	/// - Parameter resourceElement: The XMLElement of the resource to be added.
	public func add(resourceElement: XMLElement) {
		if self.fcpxResourceList == nil {
			self.rootElement()?.addChild(XMLElement(name: "resources"))
		}
		
		self.fcpxResourceList?.addChild(resourceElement)
	}
	
	
	/// Adds an array of resource XMLElements to the FCPXML document.
	///
	/// - Parameter resourceElements: An array of resource XMLElement instances.
	public func add(resourceElements: [XMLElement]) {
		for resource in resourceElements {
			self.add(resourceElement: resource)
		}
	}
	
	
	/// Removes all resources from the FCPXML document.
	public func removeAllResources() {
		self.fcpxResourceList?.setChildren(nil)
	}
	
	
	/// Adds an event to the library XMLElement of the FCPXML document.
	///
	/// - Parameter event: The XMLElement of the event to be added.
	public func add(event: XMLElement) {
		if self.fcpxLibrary == nil {
			self.rootElement()?.addChild(XMLElement(name: "library"))
		}
		
		self.fcpxLibrary?.addChild(event)
	}
	
	
	/// Adds an array of event XMLElements to the FCPXML document.
	///
	/// - Parameter events: An array of event XMLElement instances.
	public func add(events: [XMLElement]) {
		for event in events {
			self.add(event: event)
		}
	}
	
	
	/// Removes all events from the library.
	public func removeAllEvents() {
		for event in self.fcpxEvents {
			self.fcpxLibrary?.removeChild(at: event.index)
		}
		
	}
