diff --git a/aep-schedule-generator/src/data/group.rs b/aep-schedule-generator/src/data/group.rs
index 3d6dbde613a637232c13e7d43494ca2f0b650811..36417d5c7fd4e86811dcf62400273f535e2c7d98 100644
--- a/aep-schedule-generator/src/data/group.rs
+++ b/aep-schedule-generator/src/data/group.rs
@@ -31,8 +31,7 @@ impl Group {
                 let new_hour = new_period.hours | p.hours;
                 let is_mergeable = p.day == new_period.day
                     && p.room == new_period.room
-                    && (p.hours.start() == new_hour.start() + 4
-                        || p.hours.end() + 4 == new_hour.end());
+                    && (p.hours.is_contiguous(new_hour));
                 let keep = !is_mergeable || new_hours == Hours::default();
                 if is_mergeable {
                     new_hours |= new_hour;
@@ -49,8 +48,7 @@ impl Group {
                         let new_hour = new_period.hours | p.hours;
                         p.day == new_period.day
                             && p.room == new_period.room
-                            && (p.hours.start() == new_hour.start() + 4
-                                || p.hours.end() + 4 == new_hour.end())
+                            && (p.hours.is_contiguous(new_hour))
                     })
                     .unwrap();
                 first_period.hours |= new_hours;
diff --git a/aep-schedule-generator/src/data/time/hours.rs b/aep-schedule-generator/src/data/time/hours.rs
index be21f741fc81c800fe3041a54b2f67de1942d08e..2901ec49e333764f8c634e8e5e07c7e2ece189e0 100644
--- a/aep-schedule-generator/src/data/time/hours.rs
+++ b/aep-schedule-generator/src/data/time/hours.rs
@@ -53,6 +53,10 @@ impl Hours {
         (self.end() >> 2) + 8
     }
 
+    pub fn is_contiguous(self, other: Self) -> bool {
+        self.start().abs_diff(other.start()) <= 5 || self.end().abs_diff(other.end()) <= 5
+    }
+
     pub fn starting_text(self) -> String {
         let mut hour = self.starting_hour().to_string();
         let minute = self.start_minutes();