public function toIcal($starttime = null, $endtime = null, $filename = 'calendar.ics') {

		if (is_null($starttime)) {
			$starttime = time();
		}
		if (is_null($endtime)) {
			$endtime = strtotime('+1 year', $starttime);
		}

		$instances = $this->getAllEventInstances($starttime, $endtime, true, 'ical');

		$config = array(
			'unique_id' => $this->guid,
			// setting these explicitly until icalcreator bug #14 is solved
			'allowEmpty' => true,
			'nl' => "\r\n",
			'format' => 'iCal',
			'delimiter' => DIRECTORY_SEPARATOR,
			'filename' => $filename, // this is last until #14 is solved
		);

		$v = new vcalendar($config);
		$v->setProperty('method', 'PUBLISH');
		$v->setProperty("X-WR-CALNAME", implode(' - ', array(elgg_get_config('sitename'), $this->getDisplayName())));
		$v->setProperty("X-WR-CALDESC", strip_tags($this->description));
		$v->setProperty("X-WR-TIMEZONE", Util::UTC);
		foreach ($instances as $instance) {

			$e = & $v->newComponent('vevent');

			$organizer = elgg_extract('organizer', $instance, array());
			unset($instance['organizer']);

			$reminders = elgg_extract('reminders', $instance, array());
			unset($instance['reminders']);

			foreach ($instance as $property => $value) {
				$e->setProperty($property, $value);
			}

			if (!empty($organizer)) {
				if (is_email_address($organizer)) {
					$e->setProperty('organizer', $organizer);
				} else {
					$e->setProperty('organizer', elgg_get_site_entity()->email, array(
						'CN' => $organizer,
					));
				}
			}

			if (!empty($reminders)) {
				foreach ($reminders as $reminder) {
					$a = & $e->newComponent('valarm');
					$a->setProperty('action', 'DISPLAY');
					$a->setProperty('trigger', "-PT{$reminder}S");
				}
			}
		}

		$v->returnCalendar();
	}