protected function insertIntoTemplate ($template, $field, $reference = null, $after = true)
	{
		$template = $this->getTemplate($template);
		$fieldgroup = $template->fieldgroup;
		$method = $after ? 'insertAfter' : 'insertBefore';

		$field = $this->getField($field);

		// Get reference if supplied
		if($reference instanceof Field)	
			$reference = $fieldgroup->get($reference->name);
		else if(is_string($reference))  
			$reference = $fieldgroup->get($reference);

		// Insert field or append
		if($reference instanceof Field)
			$fieldgroup->$method($field, $reference);
		else 
			$fieldgroup->append($field);

		$fieldgroup->save();
	}