diff --git a/collectors/cgroups.plugin/cgroup-top.c b/collectors/cgroups.plugin/cgroup-top.c
index 5618b95cac..cb5cd9d54b 100644
--- a/collectors/cgroups.plugin/cgroup-top.c
+++ b/collectors/cgroups.plugin/cgroup-top.c
@@ -122,6 +122,8 @@ int cgroup_function_cgroup_top(BUFFER *wb, int timeout __maybe_unused, const cha
     double max_net_received = 0.0;
     double max_net_sent = 0.0;
 
+    RRDDIM *rd = NULL;
+
     uv_mutex_lock(&cgroup_root_mutex);
 
     for(struct cgroup *cg = cgroup_root; cg ; cg = cg->next) {
@@ -142,32 +144,13 @@ int cgroup_function_cgroup_top(BUFFER *wb, int timeout __maybe_unused, const cha
             cpu = cg->st_cpu_rd_user->collector.last_stored_value + cg->st_cpu_rd_system->collector.last_stored_value;
             max_cpu = MAX(max_cpu, cpu);
         }
-        buffer_json_add_array_item_double(wb, cpu);
 
-        double ram = NAN;
-        if (cg->st_mem_rd_ram) {
-            ram = cg->st_mem_rd_ram->collector.last_stored_value;
-            max_ram = MAX(max_ram, ram);
-        }
-        buffer_json_add_array_item_double(wb, ram);
+        double ram = rrddim_get_last_stored_value(cg->st_mem_rd_ram, &max_ram, 1.0);
 
-        double disk_io_read = NAN;
-        double disk_io_written = NAN;
-        if (cg->st_throttle_io_rd_read) {
-            disk_io_read = (cg->st_throttle_io_rd_read->collector.last_stored_value / 1024.0);
-            disk_io_written = (ABS(cg->st_throttle_io_rd_written->collector.last_stored_value) / 1024.0);
-        } else if (cg->st_io_rd_read) {
-            disk_io_read = (cg->st_io_rd_read->collector.last_stored_value / 1024.0);
-            disk_io_written = (ABS(cg->st_io_rd_written->collector.last_stored_value) / 1024.0);
-        }
-
-        if (!isnan(disk_io_read) && !isnan(disk_io_written)) {
-            max_disk_io_read = MAX(max_disk_io_read, disk_io_read);
-            max_disk_io_written = MAX(max_disk_io_written, disk_io_written);
-        }
-
-        buffer_json_add_array_item_double(wb, disk_io_read);
-        buffer_json_add_array_item_double(wb, disk_io_written);
+        rd = cg->st_throttle_io_rd_read ? cg->st_throttle_io_rd_read : cg->st_io_rd_read;
+        double disk_io_read = rrddim_get_last_stored_value(rd, &max_disk_io_read, 1024.0);
+        rd = cg->st_throttle_io_rd_written ? cg->st_throttle_io_rd_written : cg->st_io_rd_written;
+        double disk_io_written = rrddim_get_last_stored_value(rd, &max_disk_io_written, 1024.0);
 
 
         NETDATA_DOUBLE received, sent;
@@ -179,6 +162,10 @@ int cgroup_function_cgroup_top(BUFFER *wb, int timeout __maybe_unused, const cha
             max_net_sent = MAX(max_net_sent, sent);
         }
 
+        buffer_json_add_array_item_double(wb, cpu);
+        buffer_json_add_array_item_double(wb, ram);
+        buffer_json_add_array_item_double(wb, disk_io_read);
+        buffer_json_add_array_item_double(wb, disk_io_written);
         buffer_json_add_array_item_double(wb, received);
         buffer_json_add_array_item_double(wb, sent);
 
@@ -367,6 +354,8 @@ int cgroup_function_systemd_top(BUFFER *wb, int timeout __maybe_unused, const ch
     double max_disk_io_read = 0.0;
     double max_disk_io_written = 0.0;
 
+    RRDDIM *rd = NULL;
+
     uv_mutex_lock(&cgroup_root_mutex);
 
     for(struct cgroup *cg = cgroup_root; cg ; cg = cg->next) {
@@ -382,30 +371,16 @@ int cgroup_function_systemd_top(BUFFER *wb, int timeout __maybe_unused, const ch
             cpu = cg->st_cpu_rd_user->collector.last_stored_value + cg->st_cpu_rd_system->collector.last_stored_value;
             max_cpu = MAX(max_cpu, cpu);
         }
+
+        double ram = rrddim_get_last_stored_value(cg->st_mem_rd_ram, &max_ram, 1.0);
+
+        rd = cg->st_throttle_io_rd_read ? cg->st_throttle_io_rd_read : cg->st_io_rd_read;
+        double disk_io_read = rrddim_get_last_stored_value(rd, &max_disk_io_read, 1024.0);
+        rd = cg->st_throttle_io_rd_written ? cg->st_throttle_io_rd_written : cg->st_io_rd_written;
+        double disk_io_written = rrddim_get_last_stored_value(rd, &max_disk_io_written, 1024.0);
+
         buffer_json_add_array_item_double(wb, cpu);
-
-        double ram = NAN;
-        if (cg->st_mem_rd_ram) {
-            ram = cg->st_mem_rd_ram->collector.last_stored_value;
-            max_ram = MAX(max_ram, ram);
-        }
         buffer_json_add_array_item_double(wb, ram);
-
-        double disk_io_read = NAN;
-        double disk_io_written = NAN;
-        if (cg->st_throttle_io_rd_read) {
-            disk_io_read = (cg->st_throttle_io_rd_read->collector.last_stored_value / 1024.0);
-            disk_io_written = (ABS(cg->st_throttle_io_rd_written->collector.last_stored_value) / 1024.0);
-        } else if (cg->st_io_rd_read) {
-            disk_io_read = (cg->st_io_rd_read->collector.last_stored_value / 1024.0);
-            disk_io_written = (ABS(cg->st_io_rd_written->collector.last_stored_value) / 1024.0);
-        }
-
-        if (!isnan(disk_io_read) && !isnan(disk_io_written)) {
-            max_disk_io_read = MAX(max_disk_io_read, disk_io_read);
-            max_disk_io_written = MAX(max_disk_io_written, disk_io_written);
-        }
-
         buffer_json_add_array_item_double(wb, disk_io_read);
         buffer_json_add_array_item_double(wb, disk_io_written);
 
diff --git a/collectors/diskspace.plugin/plugin_diskspace.c b/collectors/diskspace.plugin/plugin_diskspace.c
index 9515c60e8f..365ee3bfb7 100644
--- a/collectors/diskspace.plugin/plugin_diskspace.c
+++ b/collectors/diskspace.plugin/plugin_diskspace.c
@@ -630,22 +630,6 @@ static void diskspace_main_cleanup(void *ptr) {
 #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 3
 #endif
 
-static double get_last_stored_value(RRDDIM *rd_dim, double *max_value, double mul, double div) {
-    if (!rd_dim)
-        return NAN;
-
-    if (isnan(mul) || mul == 0)
-        mul = 1.0;
-    if (isnan(div) || div == 0)
-        div = 1.0;
-
-    double value = rd_dim->collector.last_stored_value * mul / div;
-    value = ABS(value);
-    *max_value = MAX(*max_value, value);
-
-    return value;
-}
-
 int diskspace_function_mount_points(BUFFER *wb, int timeout __maybe_unused, const char *function __maybe_unused,
         void *collector_data __maybe_unused,
         rrd_function_result_callback_t result_cb, void *result_cb_data,
@@ -685,12 +669,12 @@ int diskspace_function_mount_points(BUFFER *wb, int timeout __maybe_unused, cons
         buffer_json_add_array_item_string(wb, string2str(mp->filesystem));
         buffer_json_add_array_item_string(wb, string2str(mp->mountroot));
 
-        double space_avail = get_last_stored_value(mp->rd_space_avail, &max_space_avail, 1.0, 1.0);
-        double space_used = get_last_stored_value(mp->rd_space_used, &max_space_used, 1.0, 1.0);
-        double space_reserved = get_last_stored_value(mp->rd_space_reserved, &max_space_reserved, 1.0, 1.0);
-        double inodes_avail = get_last_stored_value(mp->rd_space_avail, &max_space_avail, 1.0, 1.0);
-        double inodes_used = get_last_stored_value(mp->rd_space_used, &max_space_used, 1.0, 1.0);
-        double inodes_reserved = get_last_stored_value(mp->rd_space_reserved, &max_space_reserved, 1.0, 1.0);
+        double space_avail = rrddim_get_last_stored_value(mp->rd_space_avail, &max_space_avail, 1.0);
+        double space_used = rrddim_get_last_stored_value(mp->rd_space_used, &max_space_used, 1.0);
+        double space_reserved = rrddim_get_last_stored_value(mp->rd_space_reserved, &max_space_reserved, 1.0);
+        double inodes_avail = rrddim_get_last_stored_value(mp->rd_inodes_avail, &max_inodes_avail, 1.0);
+        double inodes_used = rrddim_get_last_stored_value(mp->rd_inodes_used, &max_inodes_used, 1.0);
+        double inodes_reserved = rrddim_get_last_stored_value(mp->rd_inodes_reserved, &max_inodes_reserved, 1.0);
 
         double space_util = NAN;
         if (!isnan(space_avail) && !isnan(space_used)) {
diff --git a/collectors/proc.plugin/proc_diskstats.c b/collectors/proc.plugin/proc_diskstats.c
index b0b310889c..64257f9a01 100644
--- a/collectors/proc.plugin/proc_diskstats.c
+++ b/collectors/proc.plugin/proc_diskstats.c
@@ -1054,102 +1054,44 @@ static int diskstats_function_block_devices(BUFFER *wb, int timeout __maybe_unus
         buffer_json_add_array_item_string(wb, d->serial);
 
         // IO
-        double io_reads = NAN;
-        if (d->rd_io_reads) {
-            io_reads = d->rd_io_reads->collector.last_stored_value / 1024.0;
-            max_io_reads = MAX(max_io_reads, io_reads);
-        }
-        buffer_json_add_array_item_double(wb, io_reads);
-
-        double io_writes = NAN;
-        if (d->rd_io_writes) {
-            io_writes = ABS(d->rd_io_writes->collector.last_stored_value / 1024.0);
-            max_io_writes = MAX(max_io_writes, io_writes);
-        }
-        buffer_json_add_array_item_double(wb, io_writes);
-
+        double io_reads = rrddim_get_last_stored_value(d->rd_io_reads, &max_io_reads, 1024.0);
+        double io_writes = rrddim_get_last_stored_value(d->rd_io_writes, &max_io_writes, 1024.0);
         double io_total = NAN;
         if (!isnan(io_reads) && !isnan(io_writes)) {
             io_total = io_reads + io_writes;
             max_io = MAX(max_io, io_total);
         }
-        buffer_json_add_array_item_double(wb, io_total);
-
         // Backlog and Busy Time
-        double busy_perc = NAN;
-        if (d->rd_util_utilization) {
-            busy_perc = d->rd_util_utilization->collector.last_stored_value;
-            max_busy_perc = MAX(max_busy_perc, busy_perc);
-        }
-        buffer_json_add_array_item_double(wb, busy_perc);
-        double busy_time = NAN;
-        if (d->rd_busy_busy) {
-            busy_time = d->rd_busy_busy->collector.last_stored_value;
-            max_busy_time = MAX(max_busy_time, busy_time);
-        }
-        buffer_json_add_array_item_double(wb, busy_time);
-        double backlog_time = NAN;
-        if (d->rd_backlog_backlog) {
-            backlog_time = d->rd_backlog_backlog->collector.last_stored_value;
-            max_backlog_time = MAX(max_backlog_time, backlog_time);
-        }
-        buffer_json_add_array_item_double(wb, backlog_time);
-
+        double busy_perc = rrddim_get_last_stored_value(d->rd_util_utilization, &max_busy_perc, 1);
+        double busy_time = rrddim_get_last_stored_value(d->rd_busy_busy, &max_busy_time, 1);
+        double backlog_time = rrddim_get_last_stored_value(d->rd_backlog_backlog, &max_backlog_time, 1);
         // IOPS
-        double iops_reads = NAN;
-        if (d->rd_ops_reads) {
-            iops_reads = d->rd_ops_reads->collector.last_stored_value;
-            max_iops_reads = MAX(max_iops_reads, iops_reads);
-        }
-        buffer_json_add_array_item_double(wb, iops_reads);
-        double iops_writes = NAN;
-        if (d->rd_ops_writes) {
-            iops_writes = ABS(d->rd_ops_writes->collector.last_stored_value);
-            max_iops_writes = MAX(max_iops_writes, iops_writes);
-        }
-        buffer_json_add_array_item_double(wb, iops_writes);
-
+        double iops_reads = rrddim_get_last_stored_value(d->rd_ops_reads, &max_iops_reads, 1);
+        double iops_writes = rrddim_get_last_stored_value(d->rd_ops_writes, &max_iops_writes, 1);
         // IO Time
-        double iops_time_reads = NAN;
-        if (d->rd_iotime_reads) {
-            iops_time_reads = d->rd_iotime_reads->collector.last_stored_value;
-            max_iops_time_reads = MAX(max_iops_time_reads, iops_time_reads);
-        }
-        buffer_json_add_array_item_double(wb, iops_time_reads);
-
-        double iops_time_writes = NAN;
-        if (d->rd_iotime_writes) {
-            iops_time_writes = ABS(d->rd_iotime_writes->collector.last_stored_value);
-            max_iops_time_writes = MAX(max_iops_time_writes, iops_time_writes);
-        }
-        buffer_json_add_array_item_double(wb, iops_time_writes);
-
+        double iops_time_reads = rrddim_get_last_stored_value(d->rd_iotime_reads, &max_iops_time_reads, 1);
+        double iops_time_writes = rrddim_get_last_stored_value(d->rd_iotime_writes, &max_iops_time_writes, 1);
         // Avg IO Time
-        double iops_avg_time_read = NAN;
-        if (d->rd_await_reads) {
-            iops_avg_time_read = d->rd_await_reads->collector.last_stored_value;
-            max_iops_avg_time_read = MAX(max_iops_avg_time_read, iops_avg_time_read);
-        }
-        buffer_json_add_array_item_double(wb, iops_avg_time_read);
-        double iops_avg_time_write = NAN;
-        if (d->rd_await_writes) {
-            iops_avg_time_write = ABS(d->rd_await_writes->collector.last_stored_value);
-            max_iops_avg_time_write = MAX(max_iops_avg_time_write, iops_avg_time_write);
-        }
-        buffer_json_add_array_item_double(wb, iops_avg_time_write);
-
+        double iops_avg_time_read = rrddim_get_last_stored_value(d->rd_await_reads, &max_iops_avg_time_read, 1);
+        double iops_avg_time_write = rrddim_get_last_stored_value(d->rd_await_writes, &max_iops_avg_time_write, 1);
         // Avg IO Size
-        double iops_avg_size_read = NAN;
-        if (d->rd_avgsz_reads) {
-            iops_avg_size_read = d->rd_avgsz_reads->collector.last_stored_value;
-            max_iops_avg_size_read = MAX(max_iops_avg_size_read, iops_avg_size_read);
-        }
+        double iops_avg_size_read = rrddim_get_last_stored_value(d->rd_avgsz_reads, &max_iops_avg_size_read, 1);
+        double iops_avg_size_write = rrddim_get_last_stored_value(d->rd_avgsz_writes, &max_iops_avg_size_write, 1);
+
+
+        buffer_json_add_array_item_double(wb, io_reads);
+        buffer_json_add_array_item_double(wb, io_writes);
+        buffer_json_add_array_item_double(wb, io_total);
+        buffer_json_add_array_item_double(wb, busy_perc);
+        buffer_json_add_array_item_double(wb, busy_time);
+        buffer_json_add_array_item_double(wb, backlog_time);
+        buffer_json_add_array_item_double(wb, iops_reads);
+        buffer_json_add_array_item_double(wb, iops_writes);
+        buffer_json_add_array_item_double(wb, iops_time_reads);
+        buffer_json_add_array_item_double(wb, iops_time_writes);
+        buffer_json_add_array_item_double(wb, iops_avg_time_read);
+        buffer_json_add_array_item_double(wb, iops_avg_time_write);
         buffer_json_add_array_item_double(wb, iops_avg_size_read);
-        double iops_avg_size_write = NAN;
-        if (d->rd_avgsz_writes) {
-            iops_avg_size_write = ABS(d->rd_avgsz_writes->collector.last_stored_value);
-            max_iops_avg_size_write = MAX(max_iops_avg_size_write, iops_avg_size_write);
-        }
         buffer_json_add_array_item_double(wb, iops_avg_size_write);
 
         // End
diff --git a/collectors/proc.plugin/proc_net_dev.c b/collectors/proc.plugin/proc_net_dev.c
index 0f878890aa..6eb9842b83 100644
--- a/collectors/proc.plugin/proc_net_dev.c
+++ b/collectors/proc.plugin/proc_net_dev.c
@@ -658,18 +658,21 @@ int netdev_function_net_interfaces(BUFFER *wb, int timeout __maybe_unused, const
         buffer_json_add_array_item_double(wb, d->mtu > 0 ? d->mtu : NAN);
 
         rd = d->flipped ? d->rd_tbytes : d->rd_rbytes;
-        double traffic_rx = NAN;
-        if (rd) {
-            traffic_rx = rd->collector.last_stored_value / 1000.0;
-            max_traffic_rx = MAX(max_traffic_rx, traffic_rx);
-        }
-
+        double traffic_rx = rrddim_get_last_stored_value(rd, &max_traffic_rx, 1000.0);
         rd = d->flipped ? d->rd_rbytes : d->rd_tbytes;
-        double traffic_tx = NAN;
-        if (rd) {
-            traffic_tx = ABS(rd->collector.last_stored_value / 1000.0);
-            max_traffic_tx = MAX(max_traffic_tx, traffic_tx);
-        }
+        double traffic_tx = rrddim_get_last_stored_value(rd, &max_traffic_tx, 1000.0);
+
+        rd = d->flipped ? d->rd_tpackets : d->rd_rpackets;
+        double packets_rx = rrddim_get_last_stored_value(rd, &max_packets_rx, 1000.0);
+        rd = d->flipped ? d->rd_rpackets : d->rd_tpackets;
+        double packets_tx = rrddim_get_last_stored_value(rd, &max_packets_tx, 1000.0);
+
+        double mcast_rx = rrddim_get_last_stored_value(d->rd_rmulticast, &max_mcast_rx, 1000.0);
+
+        rd = d->flipped ? d->rd_tdrops : d->rd_rdrops;
+        double drops_rx = rrddim_get_last_stored_value(rd, &max_drops_rx, 1.0);
+        rd = d->flipped ? d->rd_rdrops : d->rd_tdrops;
+        double drops_tx = rrddim_get_last_stored_value(rd, &max_drops_tx, 1.0);
 
         // FIXME: "traffic" (total) is needed only for default_sorting
         // can be removed when default_sorting will accept multiple columns (sum)
@@ -679,48 +682,14 @@ int netdev_function_net_interfaces(BUFFER *wb, int timeout __maybe_unused, const
             max_traffic = MAX(max_traffic, traffic);
         }
 
+
         buffer_json_add_array_item_double(wb, traffic_rx);
         buffer_json_add_array_item_double(wb, traffic_tx);
         buffer_json_add_array_item_double(wb, traffic);
-
-        rd = d->flipped ? d->rd_tpackets : d->rd_rpackets;
-        double packets_rx = NAN;
-        if (rd) {
-            packets_rx = rd->collector.last_stored_value / 1000.0;
-            max_packets_rx = MAX(max_packets_rx, packets_rx);
-        }
         buffer_json_add_array_item_double(wb, packets_rx);
-
-        rd = d->flipped ? d->rd_rpackets : d->rd_tpackets;
-        double packets_tx = NAN;
-        if (rd) {
-            packets_tx = ABS(rd->collector.last_stored_value / 1000.0);
-            max_packets_tx = MAX(max_packets_tx, packets_tx);
-        }
         buffer_json_add_array_item_double(wb, packets_tx);
-
-        double mcast_rx = NAN;
-        if (d->rd_rmulticast) {
-            mcast_rx = ABS(d->rd_rmulticast->collector.last_stored_value / 1000.0);
-            max_mcast_rx = MAX(max_mcast_rx, mcast_rx);
-        }
         buffer_json_add_array_item_double(wb, mcast_rx);
-
-
-        rd = d->flipped ? d->rd_tdrops : d->rd_rdrops;
-        double drops_rx = NAN;
-        if (rd) {
-            drops_rx = rd->collector.last_stored_value;
-            max_drops_rx = MAX(max_drops_rx, drops_rx);
-        }
         buffer_json_add_array_item_double(wb, drops_rx);
-
-        rd = d->flipped ? d->rd_rdrops : d->rd_tdrops;
-        double drops_tx = NAN;
-        if (rd) {
-            drops_tx = ABS(rd->collector.last_stored_value);
-            max_drops_tx = MAX(max_drops_tx, drops_tx);
-        }
         buffer_json_add_array_item_double(wb, drops_tx);
 
         buffer_json_add_array_item_object(wb);
diff --git a/database/rrd.h b/database/rrd.h
index 17ef5d544b..3687497ffd 100644
--- a/database/rrd.h
+++ b/database/rrd.h
@@ -1668,6 +1668,22 @@ void rrdset_pluginsd_receive_unslot_and_cleanup(RRDSET *st);
 void rrdset_pluginsd_receive_unslot(RRDSET *st);
 
 // ----------------------------------------------------------------------------
+static inline double rrddim_get_last_stored_value(RRDDIM *rd_dim, double *max_value, double div) {
+    if (!rd_dim)
+        return NAN;
+
+    if (isnan(div) || div == 0.0)
+        div = 1.0;
+
+    double value = rd_dim->collector.last_stored_value / div;
+    value = ABS(value);
+
+    *max_value = MAX(*max_value, value);
+
+    return value;
+}
+
+//
 // RRD DB engine declarations
 
 #ifdef ENABLE_DBENGINE