 	debounce[debounceIdx++] = pressed;
 	debounceIdx &= 3;
 	
-	keys.down = debounce[0] & debounce[1] & debounce[2] & debounce[3];
-	keys.up = debounce[0] | debounce[1] | debounce[2] | debounce[3];
+	down = debounce[0] & debounce[1] & debounce[2] & debounce[3];
+	up = debounce[0] | debounce[1] | debounce[2] | debounce[3];
 	
-	// Determine which buttons have freshly been pressed and return the result
+	// Determine which buttons have freshly been pressed
 	uint8_t prevStates = states;
-	states &= keys.up;
-	states |= keys.down;
+	states &= up;
+	states |= down;
 	keys.justPressed = states & (states ^ prevStates);
 	
+	// Now test which keys have been pressed for a long time
+	keys.longPressed = 0x00;
 	if(states != 0) {
-		if(states != prevStates) {
+		if(longPressDetect) {
+			// We are already in the middle of a long-press
+			
+			// Don't do anything unless we are repeating
+			if(repeat) {
+				keys.justPressed = up;
+			}
+		} else if(states != prevStates) {
+			// A button has just been pressed. Mark the time at which this happened
 			timeInStates = millis();
-		} else {
-			if(millis() - timeInStates > LONG_PRESS_MILLIS) {
-				keys.longPressed = keys.up;
-			}
+			longPressDetect = false;
+		} else if(millis() - timeInStates > repeatAfter) {
+			// Enough time has elapsed for a long press - mark it as such
+			keys.longPressed = up;
+			longPressDetect = true;
 		}
 	} else {
-		keys.longPressed = 0;
+		longPressDetect = false;
 	}
 	
 	return &keys;

	}
}

// Returns a byte with the idx of any newly pressed keys set to '1'. Also includes debouncing.
struct KeyStates *getKeys() {
	static uint8_t debounce[4];			// Stores the queue of the push button states
	static uint8_t debounceIdx = 0;		// Stores the index we are current at within the debounce queue
	static uint8_t states;				// Stores the state of the keys
	static long timeInStates = 0;		// Records the amount of time <states> has been in its current state
	
	static KeyStates keys;
	
	uint8_t pressed = 0;				// Holds which buttons are currently pressed
	
	if(digitalRead(BUTTON_L) == LOW) {
		pressed |= BUTTON_L_IDX;

	digitalWrite(PIN_LOW, LOW);
	
	// Start the serial port
	Serial.begin(115200);
	
	// Initialise the display
	disp_init();
