Replace the endless if-else if statement with an array lookup and a loop