Index: lab4/lab4.c
===================================================================
--- lab4/lab4.c	(revision 79)
+++ lab4/lab4.c	(revision 80)
@@ -35,9 +35,6 @@
   return 0;
 }
 
-extern uint8_t packet[3];
-extern int counter;
-
 int (mouse_test_packet)(uint32_t cnt) {
     int ret = 0;
     /// loop stuff
@@ -65,8 +62,8 @@
                 case HARDWARE: /* hardware interrupt notification */
                     if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
                         mouse_ih();
-                        if(counter >= 3){
-                            struct packet pp = mouse_parse_packet(packet);
+                        if(counter_mouse_ih >= 3){
+                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
                             mouse_print_packet(&pp);
                             cnt_now++;
                             if(cnt == cnt_now) good = 0;
@@ -86,21 +83,16 @@
 }
 
 int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
-
-    // Mouse packets data
-    uint8_t packet[3];
-    int sz = 0;
-    uint8_t data = 0;
-    // Cycle
-    int packetCounter = 0;
-    int good = 1;
-    // return value
     int ret;
+    //if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret;
+    //if ((ret = mouse_set_data_report(true))) return ret;
 
-    if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret;
-
-    if ((ret = mouse_set_data_report(true))) return ret;
-
+    struct packet pp;
+    while(cnt--){
+        if(mouse_poll(&pp, period)) return 1;
+        mouse_print_packet(&pp);
+    }
+    /*
     while (good) {
 
         if ((ret = mouse_read_data(&data))) return ret;
@@ -118,7 +110,7 @@
         }
         tickdelay(micros_to_ticks(period*1e3));
     }
-
+    */
     // Set Stream mode
     if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
     // Disable data reporting
@@ -167,8 +159,8 @@
                     }
                     if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
                         mouse_ih();
-                        if(counter >= 3){
-                            struct packet pp = mouse_parse_packet(packet);
+                        if(counter_mouse_ih >= 3){
+                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
                             mouse_print_packet(&pp);
                             time = 0;
                             no_interrupts = 0;
@@ -221,8 +213,8 @@
                 case HARDWARE: /* hardware interrupt notification */
                     if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
                         mouse_ih();
-                        if(counter >= 3) {
-                            pp = mouse_parse_packet(packet);
+                        if(counter_mouse_ih >= 3) {
+                            pp = mouse_parse_packet(packet_mouse_ih);
                             mouse_print_packet(&pp);
                             event = mouse_get_event(&pp);
 
Index: lab4/mouse.c
===================================================================
--- lab4/mouse.c	(revision 79)
+++ lab4/mouse.c	(revision 80)
@@ -15,13 +15,12 @@
 }
 
 int got_error_mouse_ih = 0;
-uint8_t packet[3];
-int counter = 0;
+int counter_mouse_ih = 0;
 
 void (mouse_ih)(void) {
     uint8_t status = 0;
     got_error_mouse_ih = 0;
-    if(counter >= 3) counter = 0;
+    if(counter_mouse_ih >= 3) counter_mouse_ih = 0;
 
     if ((got_error_mouse_ih = util_sys_inb(STATUS_REG, &status))) return;
 
@@ -39,9 +38,9 @@
     if ((got_error_mouse_ih = util_sys_inb(OUTPUT_BUF, &byte))) return;
 
     /// This does not run if: I was expecting the first one but what I get is definitely not the first byte
-    if((byte & FIRST_BYTE_ID)  || counter){
-        packet[counter] = byte;
-        counter++;
+    if((byte & FIRST_BYTE_ID)  || counter_mouse_ih){
+        packet_mouse_ih[counter_mouse_ih] = byte;
+        counter_mouse_ih++;
     }
 }
 
@@ -60,21 +59,32 @@
     return pp;
 }
 
-int (mouse_set_data_report)(int on){
+int mouse_poll(struct packet *pp, uint16_t period){
     int ret = 0;
-    if(on){
-        if((ret = mouse_issue_cmd(ENABLE_DATA_REP))) return ret;
+
+    int counter = 0;
+    uint8_t packet[3];
+    uint8_t byte;
+    while(counter < 3){
+        if((ret = mouse_read_data(&byte, period))) return ret;
+        if((byte & FIRST_BYTE_ID) || counter){
+            packet[counter] = byte;
+            counter++;
+        }
     }
-    else{
-        if((ret = mouse_issue_cmd(   DIS_DATA_REP))) return ret;
-    }
-    return ret;
+    *pp = mouse_parse_packet(packet);
+    return SUCCESS;
 }
 
-int (mouse_read_data)(uint8_t *data) {
+int (mouse_set_data_report)(int on){
+    if(on) return mouse_issue_cmd(ENABLE_DATA_REP);
+    else   return mouse_issue_cmd(   DIS_DATA_REP);
+}
+
+int (mouse_read_data)(uint8_t *data, uint16_t period) {
     int ret;
     if ((ret = mouse_issue_cmd(READ_DATA))) return ret;
-    if ((ret = mouse_read_byte(data))) return ret;
+    if ((ret = mouse_poll_byte(data, period))) return ret;
     return SUCCESS;
 }
 
@@ -123,6 +133,20 @@
     //return TIMEOUT_ERROR;
 }
 
+int (mouse_poll_byte)(uint8_t *byte, uint16_t period) {
+    int ret = 0;
+    uint8_t stat;
+    while(true){
+        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
+        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)) {
+            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR;
+            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
+            else return SUCCESS;
+        }
+        tickdelay(micros_to_ticks(period*1000));
+    }
+}
+
 int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte) {
     return (int16_t)(((0xFF * sign_bit)<<8) | byte);
 }
Index: lab4/mouse.h
===================================================================
--- lab4/mouse.h	(revision 79)
+++ lab4/mouse.h	(revision 80)
@@ -12,8 +12,17 @@
  */
 int (subscribe_mouse_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
 
+int got_error_mouse_ih;
+uint8_t packet_mouse_ih[3];
+int counter_mouse_ih;
+
 struct packet (mouse_parse_packet)(const uint8_t *packet_bytes);
 
+int mouse_poll(struct packet *pp, uint16_t period);
+
+/**
+ *
+ */
 int (mouse_set_data_report)(int on);
 
 /**
@@ -25,7 +34,7 @@
  * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
  * @see {_ERRORS_H_::errors}
  */
-int (mouse_read_data)(uint8_t *data);
+int (mouse_read_data)(uint8_t *data, uint16_t period);
 
 /**
  * @brief Issues command to mouse
@@ -51,6 +60,8 @@
 
 int (mouse_read_ack)(uint8_t *byte);
 
+int (mouse_poll_byte)(uint8_t *byte, uint16_t period);
+
 /**
  * @brief Converts 9-bit number to 16-bit with sign extension
  * @param sign_bit  Sign bit identifiying the signal of the number
