   bool anyPressed = false;
   bool changed = false;
   for( int i=0; i<buttonsAllocated; i++ ) {
-    bool lastState = getButton(buttonCode[i]);
+    bool lastState = buttonLastState[i];
     bool buttonPressed = checkButton(i);
     if( lastState != buttonPressed) {
-      setButton(buttonCode[i], buttonPressed);
+      buttonLastState[i] = buttonPressed;
       changed = true;
     }
     anyPressed |= buttonPressed;

  }
}

void setButton(int nr, boolean isPressed) {
    int byteNr = nr / 8;
    int bitNr = nr % 8;
    
    if( isPressed ) {
        message[byteNr] |= 1 << bitNr;
    } else {
        message[byteNr] &= 255 - (1<<bitNr);
    }
}

bool getButton( int nr ) {
    int byteNr = nr / 8;
    int bitNr = nr % 8;

    return (message[byteNr] & (1<<bitNr))>0;
}
    
bool checkButton( int buttonIndex ) {
    if(digitalRead( buttonPin[buttonIndex] ) == (buttonPullup[buttonIndex]?LOW:HIGH)) {
        if(buttonThresCounter[buttonIndex] < BUTTON_THRES)

unsigned char buttonCode[MAX_NUMBER_OF_BUTTONS];
unsigned int buttonPin[MAX_NUMBER_OF_BUTTONS];
bool buttonPullup[MAX_NUMBER_OF_BUTTONS];
bool buttonLastState[MAX_NUMBER_OF_BUTTONS];
unsigned int buttonThresCounter[MAX_NUMBER_OF_BUTTONS];
int buttonsAllocated=0;
int frame = 0;
WiFiUDP udp;
IPAddress serverIP;


void init_header() {
  int pos = 0;
  for(int i=0; i<4; i++) {
    message[pos] = MAGIC[i];
    pos += 1;
  }
  long version = htonl(PROTOCOL_VERSION);
  for(int i=0; i<4; i++) {
    message[pos] = ((char *)&version)[i];
    pos += 1;
  }
  int protocol_type = htons(1); //TODO: read or define 1 elsewhere
  for(int i=0; i<2; i++) {
    message[pos] = ((char *)&protocol_type)[i];
    pos += 1;
  }
  for(int i=0; i<4; i++) {
    message[pos] = random(256);
    pos += 1;
  }
  String CLIENT_ID ="\0\0\0\2";
  for(int i=0; i<4; i++) {
    message[pos] = CLIENT_ID[i];
    pos += 1;
  }
}

void initButton( int buttonIndex, unsigned char code, int pin, bool pullup ) {
  buttonCode[ buttonIndex ] = code;
  buttonPin[ buttonIndex] = pin;
  buttonPullup[ buttonIndex ] = pullup;
  buttonLastState[ buttonIndex ] = false;
  //setButtonState(code,false); //done already
  if(pullup) pinMode(pin, INPUT_PULLUP);
  else pinMode(pin, INPUT); // needs to be manually pulled down  
