protected function loadAppLibrary($class, $subdir, array $parameters = null, array $directories, $newInstance = false) 
	{
		// First check if the input is correct
		if (!is_array($directories)) 
		{
			throw new LibraryException("Could not load library. \$directory variable was not an array", 1);
		}

		// Search for the file
		foreach ($directories as $directory) 
		{
			// Skip the core directory
			if ($directory === Core::$coreDir . DS . 'Libraries')
			{
				continue;
			}

			// Determine the file
			$file = $directory . DS . $subdir . $class . '.php';
			$className = '\Application\Library\\'.$class;

			// Check if the file was already loaded
			if (class_exists($className, false)) 
			{
				// Return existing instance
				if (!isset($this->libraries[$className])) 
				{
					return $this->initLibrary($className, $parameters);
				}

				// If required to do so, return the existing instance or load a new one
				if ($newInstance)
				{
					$this->factory->logger->log("Library '".$className."' already loaded. Returning existing instance");
					return $this->libraries[$prefix.$class];	
				}

				$this->factory->logger->log("Library '".$className."' already loaded. Returning new instance");
				return $this->initLibrary($className, $parameters);
			}

			// Otherwise load the file first
			if (file_exists($file))
			{
				include_once($file);
				return $this->initLibrary($className, $parameters);
			}
		}

		// Maybe it's in a subdirectory with the same name as the class
		if ($subdir === '')
		{
			return $this->loadLibrary($class."/".$class, $parameters, $directories, $newInstance);
		}

		throw new LibraryException("Could not load library. Library was not found", 1);
	}