diff --git a/contents/code/halflayout.js b/contents/code/halflayout.js
index c8a571036db7b21252c5a2191f6697e8563c533d..898da032399512ee6fe4c03fc5674daf2d34a503 100644
--- a/contents/code/halflayout.js
+++ b/contents/code/halflayout.js
@@ -111,6 +111,10 @@ HalfLayout.prototype.removeTile = function(tileIndex) {
 		//FIXME: There is a crash here
 		// Remove the array entry
 		var oldrect = this.tiles[tileIndex].rectangle;
+		if (tileIndex < 0 || tileIndex >= this.tiles.length) {
+			print("Removing invalid tileindex");
+			return;
+		}
 		this.tiles.splice(tileIndex, 1);
 		// Update the other tiles
 		if (this.tiles.length == 1) {