diff --git a/display.c b/display.c
index 257e540..45446cb 100644
--- a/display.c
+++ b/display.c
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <ncurses.h>
 #include "powerdebug.h"
+#include "mainloop.h"
 #include "regulator.h"
 #include "display.h"
 
@@ -106,130 +107,14 @@ static int show_header_footer(int win)
 	return 0;
 }
 
-int display_init(int wdefault)
+int display_refresh(int win)
 {
-	int i;
-	size_t array_size = sizeof(windata) / sizeof(windata[0]);
-
-	current_win = wdefault;
-
-	if (!initscr())
-		return -1;
+	/* we are trying to refresh a window which is not showed */
+	if (win != current_win)
+		return 0;
 
-	start_color();
-	use_default_colors();
-
-	keypad(stdscr, TRUE);
-	noecho();
-	cbreak();
-	curs_set(0);
-	nonl();
-
-	if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
-	    init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
-	    init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
-	    init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
-	    init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
-	    init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
-	    init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
-	    init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
-		return -1;
-
-	if (atexit(display_fini))
-		return -1;
-
-	getmaxyx(stdscr, maxy, maxx);
-
-	for (i = 0; i < array_size; i++) {
-
-		windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
-		if (!windata[i].win)
-			return -1;
-
-		windata[i].pad = newpad(maxrows, maxx);
-		if (!windata[i].pad)
-			return -1;
-
-	}
-
-	header_win = subwin(stdscr, 1, maxx, 0, 0);
-	if (!header_win)
-		return -1;
-
-	footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
-	if (!footer_win)
-		return -1;
-
-	return show_header_footer(wdefault);
-}
-
-void print_regulator_header(void)
-{
-	WINDOW *regulator_win = windata[REGULATOR].win;
-
-	werase(regulator_win);
-	wattron(regulator_win, A_BOLD);
-	print(regulator_win, 0, 0, "Name");
-	print(regulator_win, 12, 0, "Status");
-	print(regulator_win, 24, 0, "State");
-	print(regulator_win, 36, 0, "Type");
-	print(regulator_win, 48, 0, "Users");
-	print(regulator_win, 60, 0, "Microvolts");
-	print(regulator_win, 72, 0, "Min u-volts");
-	print(regulator_win, 84, 0, "Max u-volts");
-	wattroff(regulator_win, A_BOLD);
-	wrefresh(regulator_win);
-
-	show_header_footer(REGULATOR);
-}
-
-void print_clock_header(void)
-{
-	WINDOW *clock_win = windata[CLOCK].win;
-
-	werase(clock_win);
-	wattron(clock_win, A_BOLD);
-	print(clock_win, 0, 0, "Name");
-	print(clock_win, 56, 0, "Flags");
-	print(clock_win, 75, 0, "Rate");
-	print(clock_win, 88, 0, "Usecount");
-	print(clock_win, 98, 0, "Children");
-	wattroff(clock_win, A_BOLD);
-	wrefresh(clock_win);
-
-	show_header_footer(CLOCK);
-}
-
-void print_sensor_header(void)
-{
-	WINDOW *sensor_win = windata[SENSOR].win;
-
-	werase(sensor_win);
-	wattron(sensor_win, A_BOLD);
-	print(sensor_win, 0, 0, "Name");
-	print(sensor_win, 36, 0, "Value");
-	wattroff(sensor_win, A_BOLD);
-	wrefresh(sensor_win);
-
-	show_header_footer(SENSOR);
-}
-
-int display_register(int win, struct display_ops *ops)
-{
-	size_t array_size = sizeof(windata) / sizeof(windata[0]);
-
-	if (win < 0 || win >= array_size)
-		return -1;
-
-	windata[win].ops = ops;
-
-	return 0;
-}
-
-int display_refresh(void)
-{
-	if (windata[current_win].ops && windata[current_win].ops->display)
-		return windata[current_win].ops->display();
+	if (windata[win].ops && windata[win].ops->display)
+		return windata[win].ops->display();
 
 	return 0;
 }
@@ -378,9 +263,8 @@ int display_print_line(int win, int line, char *str, int bold, void *data)
 	return 0;
 }
 
-int display_keystroke(void *data)
+static int display_keystroke(int fd, void *data)
 {
-	int *tick = data;
 	int keystroke = getch();
 
 	switch (keystroke) {
@@ -414,10 +298,139 @@ int display_keystroke(void *data)
 
 	case 'r':
 	case 'R':
-		display_refresh();
-		*tick = 3;
+		/* refresh will be done after */
 		break;
+	default:
+		return 0;
+	}
+
+	display_refresh(current_win);
+
+	return 0;
+}
+
+int display_init(int wdefault)
+{
+	int i;
+	size_t array_size = sizeof(windata) / sizeof(windata[0]);
+
+	current_win = wdefault;
+
+	if (mainloop_add(0, display_keystroke, NULL))
+		return -1;
+
+	if (!initscr())
+		return -1;
+
+	start_color();
+	use_default_colors();
+
+	keypad(stdscr, TRUE);
+	noecho();
+	cbreak();
+	curs_set(0);
+	nonl();
+
+	if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
+	    init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
+	    init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
+	    init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
+	    init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
+	    init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
+	    init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
+	    init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
+		return -1;
+
+	if (atexit(display_fini))
+		return -1;
+
+	getmaxyx(stdscr, maxy, maxx);
+
+	for (i = 0; i < array_size; i++) {
+
+		windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
+		if (!windata[i].win)
+			return -1;
+
+		windata[i].pad = newpad(maxrows, maxx);
+		if (!windata[i].pad)
+			return -1;
+
 	}
 
+	header_win = subwin(stdscr, 1, maxx, 0, 0);
+	if (!header_win)
+		return -1;
+
+	footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
+	if (!footer_win)
+		return -1;
+
+	if (show_header_footer(wdefault))
+		return -1;
+
+	return display_refresh(wdefault);
+}
+
+void print_regulator_header(void)
+{
+	WINDOW *regulator_win = windata[REGULATOR].win;
+
+	werase(regulator_win);
+	wattron(regulator_win, A_BOLD);
+	print(regulator_win, 0, 0, "Name");
+	print(regulator_win, 12, 0, "Status");
+	print(regulator_win, 24, 0, "State");
+	print(regulator_win, 36, 0, "Type");
+	print(regulator_win, 48, 0, "Users");
+	print(regulator_win, 60, 0, "Microvolts");
+	print(regulator_win, 72, 0, "Min u-volts");
+	print(regulator_win, 84, 0, "Max u-volts");
+	wattroff(regulator_win, A_BOLD);
+	wrefresh(regulator_win);
+
+	show_header_footer(REGULATOR);
+}
+
+void print_clock_header(void)
+{
+	WINDOW *clock_win = windata[CLOCK].win;
+
+	werase(clock_win);
+	wattron(clock_win, A_BOLD);
+	print(clock_win, 0, 0, "Name");
+	print(clock_win, 56, 0, "Flags");
+	print(clock_win, 75, 0, "Rate");
+	print(clock_win, 88, 0, "Usecount");
+	print(clock_win, 98, 0, "Children");
+	wattroff(clock_win, A_BOLD);
+	wrefresh(clock_win);
+
+	show_header_footer(CLOCK);
+}
+
+void print_sensor_header(void)
+{
+	WINDOW *sensor_win = windata[SENSOR].win;
+
+	werase(sensor_win);
+	wattron(sensor_win, A_BOLD);
+	print(sensor_win, 0, 0, "Name");
+	print(sensor_win, 36, 0, "Value");
+	wattroff(sensor_win, A_BOLD);
+	wrefresh(sensor_win);
+
+	show_header_footer(SENSOR);
+}
+
+int display_register(int win, struct display_ops *ops)
+{
+	size_t array_size = sizeof(windata) / sizeof(windata[0]);
+
+	if (win < 0 || win >= array_size)
+		return -1;
+
+	windata[win].ops = ops;
+
 	return 0;
 }
diff --git a/display.h b/display.h
index f354195..749d8a3 100644
--- a/display.h
+++ b/display.h
@@ -29,8 +29,7 @@ extern void *display_get_row_data(int window);
 
 extern int display_init(int wdefault);
 extern int display_register(int win, struct display_ops *ops);
-extern int display_refresh(void);
-extern int display_keystroke(void *data);
+extern int display_refresh(int win);
 
 /* FIXME */
 extern void print_sensor_header(void);
diff --git a/mainloop.c b/mainloop.c
index d63d703..02dda98 100644
--- a/mainloop.c
+++ b/mainloop.c
@@ -32,7 +32,7 @@ struct mainloop_data **mds;
 
 #define MAX_EVENTS 10
 
-int _mainloop(unsigned int timeout)
+int mainloop(unsigned int timeout)
 {
         int i, nfds;
         struct epoll_event events[MAX_EVENTS];
diff --git a/mainloop.h b/mainloop.h
index 3b4c253..cf03bbb 100644
--- a/mainloop.h
+++ b/mainloop.h
@@ -15,7 +15,7 @@
 
 typedef int (*mainloop_callback_t)(int fd, void *data);
 
-extern int _mainloop(unsigned int timeout);
+extern int mainloop(unsigned int timeout);
 extern int mainloop_add(int fd, mainloop_callback_t cb, void *data);
 extern int mainloop_del(int fd);
 extern int mainloop_init(void);
diff --git a/powerdebug.c b/powerdebug.c
index 065fa31..b4575c4 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -24,6 +24,7 @@
 #include "display.h"
 #include "clocks.h"
 #include "sensor.h"
+#include "mainloop.h"
 #include "powerdebug.h"
 
 void usage(void)
@@ -157,38 +158,6 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options)
 	return 0;
 }
 
-int mainloop(struct powerdebug_options *options)
-{
-	while (1) {
-		int ret;
-		struct timeval tval;
-		fd_set readfds;
-
-		display_refresh();
-
-		FD_ZERO(&readfds);
-		FD_SET(0, &readfds);
-		tval.tv_sec = options->ticktime;
-		tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
-
-	again:
-		ret = select(1, &readfds, NULL, NULL, &tval);
-		if (!ret)
-			continue;
-
-		if (ret < 0) {
-			if (errno == EINTR)
-				goto again;
-			break;
-		}
-
-		if (display_keystroke(&options->ticktime))
-			break;
-	}
-
-	return 0;
-}
-
 static int powerdebug_dump(struct powerdebug_options *options)
 {
 	if (options->regulators)
@@ -210,10 +179,7 @@ static int powerdebug_display(struct powerdebug_options *options)
 		return -1;
 	}
 
-	if (display_refresh())
-		return -1;
-
-	if (mainloop(options))
+	if (mainloop(options->ticktime * 1000))
 		return -1;
 
 	return 0;
@@ -248,6 +214,11 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
+	if (mainloop_init()) {
+		fprintf(stderr, "failed to initialize the mainloop\n");
+		return 1;
+	}
+
 	if (regulator_init()) {
 		printf("not enough memory to allocate regulators info\n");
 		options->regulators = false;
