diff --git a/epub.php b/epub.php
index 8f38d7e..3ca211c 100644
--- a/epub.php
+++ b/epub.php
@@ -44,6 +44,15 @@
 		}
 	}
 
+	function lm_download_image($base_url, $url_part, $id)
+	{
+		$image_url = preg_replace('/GetPublicationContentItems-.*\.json/', $url_part . $id . '.jpg', $base_url);
+		$temp_img_path = './temp/' . $id . '.jpg';
+		$temp_content = file_get_contents($image_url);
+		file_put_contents($temp_img_path, $temp_content);
+		return $temp_img_path;
+	}
+
 	date_default_timezone_set('Europe/Paris');
 	$date = (new DateTime('today'))->format('Ymd');
 
@@ -103,17 +112,13 @@
 				}))[0];
 				$pageid = $pageobj->PublicationPageID;
 
-				// SMALL / MEDIUM / XLARGE
-				$pageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Preview-MEDIUM-' . $pageid . '.jpg', $url);
-				$tempimgpath = './temp/' . $pageid . '.jpg';
-				$tempcontent = file_get_contents($pageurl);
-				file_put_contents($tempimgpath, $tempcontent);
-				$epub->AddPage('', false, 'Page ' . $page, true);
+				$path = lm_download_image($url, 'Preview-MEDIUM-', $pageid);
+				$epub->AddPage('
', false, 'Page ' . $page, true);
 
 				// Add page 1 as cover
 				if ($page == 1)
 				{
-					$epub->AddImage($tempimgpath, 'image/jpeg', true);
+					$epub->AddImage($path, 'image/jpeg', true);
 				}
 			}
 
@@ -142,15 +147,8 @@
 					$images = array_values(array_filter($article->ContentItem, function($item) { return $item->ContentType == 'graphic/jpeg' || $item->ContentType == 'image/jpeg'; }));
 					foreach ($images as $image)
 					{
-						$imageid = $image->ContentItemId;
-						$imageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Image-MEDIUM-' . $imageid . '.jpg', $url);
-
-						// Download image from source, put it in temp and include it into article main html content.
-						// Then pass true to last AddPage param to download image to epub.
-						$tempimgpath = './temp/' . $imageid . '.jpg';
-						$tempcontent = file_get_contents($imageurl);
-						file_put_contents($tempimgpath, $tempcontent);
-						$pagecontent .= '