Index: src/uk/me/parabola/imgfmt/Utils.java
===================================================================
--- src/uk/me/parabola/imgfmt/Utils.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/Utils.java	(working copy)
@@ -425,5 +425,17 @@
 
 		return true;
 	}
+
+	public static int numberToPointerSize(int n) {
+	// moved from imgfmt/app/mdr/MdrSection.java and app/typ/TYPFile.java
+		if (n <= 0xff)
+			return 1;
+		else if (n <= 0xffff)
+			return 2;
+		else if (n <= 0xffffff)
+			return 3;
+		else
+			return 4;
+	}
+
 }
-
Index: src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/BufferedImgFileWriter.java	(working copy)
@@ -119,6 +119,28 @@
 	}
 
 	/**
+	 * Write out int in range 0.255 as single byte.
+	 * Use instead of put() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put1(int val) {
+		assert val >= 0 && val <= 255 : val;
+		ensureSize(1);
+		buf.put((byte)val);
+	}
+
+	/**
+	 * Write out int in range 0.65535 as 2 bytes in correct byte order.
+	 * Use instead of putChar() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put2(int val) {
+		assert val >= 0 && val <= 65535 : val;
+		ensureSize(2);
+		buf.putShort((short)val);
+	}
+
+	/**
 	 * Write out a 3 byte value in the correct byte order etc.
 	 *
 	 * @param val The value to write.
@@ -130,6 +152,33 @@
 	}
 
 	/**
+	 * Write out 1-4 bytes.  Done in the correct byte order.
+	 *
+	 * @param nBytes The number of bytes to write.
+	 * @param val The value to write.
+	 */
+	public void putN(int nBytes, int val) {
+		ensureSize(nBytes);
+		switch (nBytes) {
+		case 1:
+			buf.put((byte)val);
+			break;
+		case 2:
+			buf.putShort((short)val);
+			break;
+		case 3:
+			buf.put((byte)val);
+			buf.putShort((short)(val >> 8));
+			break;
+		case 4:
+			buf.putInt(val);
+			break;
+		default:
+			assert false : nBytes;
+		}
+	}
+
+	/**
 	 * Write out 4 byte value.
 	 *
 	 * @param val The value to write.
Index: src/uk/me/parabola/imgfmt/app/FileBackedImgFileWriter.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/FileBackedImgFileWriter.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/FileBackedImgFileWriter.java	(working copy)
@@ -130,6 +130,36 @@
 	}
 
 	/**
+	 * Write out int in range 0.255 as single byte.
+	 * Use instead of put() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put1(int val) {
+		assert val >= 0 && val <= 255 : val;
+ 		try {
+			file.write(val);
+		} catch (IOException e) {
+			throw new MapFailedException("could not write byte to mdr tmp file");
+		}
+	}
+
+	/**
+	 * Write out int in range 0.65535 as 2 bytes in correct byte order.
+	 * Use instead of putChar() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put2(int val) {
+		assert val >= 0 && val <= 65535 : val;
+ 		try {
+			file.write(val);
+			file.write(val >> 8);
+		} catch (IOException e) {
+			throw new MapFailedException("could not write 2 bytes to mdr tmp file");
+		}
+
+	}
+
+	/**
 	 * Write out three bytes.  Done in the little endian byte order.
 	 *
 	 * @param val The value to write, only the bottom three bytes will be written.
@@ -145,6 +175,29 @@
 	}
 
 	/**
+	 * Write out 1-4 bytes.  Done in the correct byte order.
+	 *
+	 * @param nBytes The number of bytes to write.
+	 * @param val The value to write.
+	 */
+	public void putN(int nBytes, int val) {
+		try {
+			file.write(val);
+			if (nBytes <= 1)
+				return;
+			file.write(val >> 8);
+			if (nBytes <= 2)
+				return;
+			file.write(val >> 16);
+			if (nBytes <= 3)
+				return;
+			file.write(val >> 24);
+		} catch (IOException e) {
+			throw new MapFailedException("could not write put3 to mdr tmp file");
+		}
+	}
+
+	/**
 	 * Write out 4 byte value.
 	 *
 	 * @param val The value to write.
Index: src/uk/me/parabola/imgfmt/app/ImgFileWriter.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/ImgFileWriter.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/ImgFileWriter.java	(working copy)
@@ -64,6 +64,20 @@
 	public void putChar(char c);
 
 	/**
+	 * Write out int in range 0.255 as single byte.
+	 * Use instead of put() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put1(int val);
+
+	/**
+	 * Write out int in range 0.65535 as two bytes in correct byte order.
+	 * Use instead of putChar() for clarity.
+	 * @param int The value to write.
+	 */
+	public void put2(int val);
+
+	/**
 	 * Write out three bytes.  Done in the correct byte order.
 	 *
 	 * @param val The value to write, only the bottom three bytes will be
@@ -72,6 +86,14 @@
 	public void put3(int val);
 	
 	/**
+	 * Write out 1-4 bytes.  Done in the correct byte order.
+	 *
+	 * @param nBytes The number of bytes to write.
+	 * @param val The value to write.
+	 */
+	public void putN(int nBytes, int val);
+
+	/**
 	 * Write out 4 byte value.
 	 * @param val The value to write.
 	 */
Index: src/uk/me/parabola/imgfmt/app/SectionWriter.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/SectionWriter.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/SectionWriter.java	(working copy)
@@ -66,10 +66,22 @@
 		writer.putChar(c);
 	}
 
+	public void put1(int val) {
+		writer.put1(val);
+	};
+
+	public void put2(int val) {
+		writer.put2(val);
+	};
+
 	public void put3(int val) {
 		writer.put3(val);
 	}
 
+	public void putN(int nBytes, int val) {
+		writer.putN(nBytes, val);
+	}
+
 	public void putInt(int val) {
 		writer.putInt(val);
 	}
Index: src/uk/me/parabola/imgfmt/app/lbl/LBLFile.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/lbl/LBLFile.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/lbl/LBLFile.java	(working copy)
@@ -226,6 +226,14 @@
 		return places.numZips();
 	}
 
+	public int numHighways() {
+		return places.numHighways();
+	}
+
+	public int numExitFacilities() {
+		return places.numExitFacilities();
+	}
+
 	public int getCodePage() {
 		return lblHeader.getCodePage();
 	}
Index: src/uk/me/parabola/imgfmt/app/lbl/LBLFileReader.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/lbl/LBLFileReader.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/lbl/LBLFileReader.java	(working copy)
@@ -19,6 +19,7 @@
 import java.util.Map;
 
 import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
 import uk.me.parabola.imgfmt.app.ImgFile;
 import uk.me.parabola.imgfmt.app.ImgFileReader;
@@ -404,22 +405,12 @@
 			}
 
 			if (hasCity) {
-				int cityIndex;
-
-				if (placeHeader.getNumCities() > 0xFF)
-					cityIndex = reader.getChar();
-				else
-					cityIndex = reader.get() & 0xff;
-
+				int cityIndex = reader.getUint(Utils.numberToPointerSize(placeHeader.getNumCities()));
 				poi.setCity(cities.get(cityIndex-1));
 			}
 
 			if (hasZip) {
-				int zipIndex;
-				if (placeHeader.getNumZips() > 0xff)
-					zipIndex = reader.getChar();
-				else
-					zipIndex = reader.get() & 0xff;
+				int zipIndex = reader.getUint(Utils.numberToPointerSize(placeHeader.getNumZips()));
 				poi.setZip(zips.get(zipIndex-1));
 			}
 			
@@ -442,12 +433,9 @@
 				boolean indexed = (lblinfo & 0x800000) != 0;
 				boolean overnightParking = (lblinfo & 0x400000) != 0;
 
-				int highwayIndex = (placeHeader.getNumHighways() > 255)
-					? reader.getChar() : reader.get();
+				int highwayIndex = reader.getUint(Utils.numberToPointerSize(placeHeader.getNumHighways()));
 				if (indexed) {
-					int eidx = (placeHeader.getNumExits() > 255) ?
-									reader.getChar() :
-									reader.get();
+					int eidx = reader.getUint(Utils.numberToPointerSize(placeHeader.getNumExits()));
 				}
 			}
 
Index: src/uk/me/parabola/imgfmt/app/lbl/POIRecord.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/lbl/POIRecord.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/lbl/POIRecord.java	(working copy)
@@ -18,6 +18,7 @@
 
 import java.util.List;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.Exit;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 import uk.me.parabola.imgfmt.app.Label;
@@ -130,18 +131,12 @@
 		if (city != null)
 		{
 			char cityIndex = (char) city.getIndex();
-			if(numCities > 255)
-				writer.putChar(cityIndex);
-			else
-				writer.put((byte)cityIndex);
+			writer.putN(Utils.numberToPointerSize((int)numCities), cityIndex);
 		}
 
 		if (zip != null) {
 			char zipIndex = (char) zip.getIndex();
-			if(numZips > 255)
-				writer.putChar(zipIndex);
-			else
-				writer.put((byte) zipIndex);
+			writer.putN(Utils.numberToPointerSize((int)numZips), zipIndex);
 		}
 
 		if (complexPhoneNumber != null)
@@ -171,17 +166,11 @@
 			writer.put3(val);
 
 			char highwayIndex = (char)exit.getHighway().getIndex();
-			if(numHighways > 255)
-				writer.putChar(highwayIndex);
-			else
-				writer.put((byte)highwayIndex);
-			
+			writer.putN(Utils.numberToPointerSize((int)numHighways), highwayIndex);
+
 			if(ef != null) {
 				char exitFacilityIndex = (char)ef.getIndex();
-				if(numExitFacilities > 255)
-					writer.putChar(exitFacilityIndex);
-				else
-					writer.put((byte)exitFacilityIndex);
+				writer.putN(Utils.numberToPointerSize((int)numExitFacilities), exitFacilityIndex);
 			}
 		}
 	}
@@ -242,9 +231,9 @@
 		int size = 3;
 		if (exit != null) {
 			size += 3;
-			size += (numHighways > 255)? 2 : 1;
+			size += Utils.numberToPointerSize((int)numHighways);
 			if(!exit.getFacilities().isEmpty())
-				size += (numExitFacilities > 255)? 2 : 1;
+				size += Utils.numberToPointerSize((int)numExitFacilities);
 		}
 		if (POIGlobalFlags != getPOIFlags())
 			size += 1;
@@ -262,20 +251,13 @@
 		{
 			/*
 			  depending on how many cities are in the LBL block we have
-			  to write one or two bytes 
+			  to write one to three bytes 
 			*/
-		
-			if(numCities > 255)
-				size += 2;
-			else
-				size += 1;
+			size += Utils.numberToPointerSize((int)numCities);
 		}
 		if (zip != null) {
-			// depending on how many zips are in the LBL block we have to write one or two bytes
-			if(numZips > 255)
-			   size += 2;						
-			else
-			   size += 1;
+			// depending on how many zips are in the LBL block we have to write one to three bytes
+			size += Utils.numberToPointerSize((int)numZips);
 		}
 		return size;
 	}
Index: src/uk/me/parabola/imgfmt/app/lbl/PlacesFile.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/lbl/PlacesFile.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/lbl/PlacesFile.java	(working copy)
@@ -419,6 +419,14 @@
 		return postalCodes.size();
 	}
 
+	public int numHighways() {
+		return highways.size();
+	}
+
+	public int numExitFacilities() {
+		return exitFacilities.size();
+	}
+
 	public void setSort(Sort sort) {
 		this.sort = sort;
 	}
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr15.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr15.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr15.java	(working copy)
@@ -132,7 +132,7 @@
 	 * for this.
 	 */
 	public int getSizeForRecord() {
-		return numberToPointerSize(nextOffset);
+		return Utils.numberToPointerSize(nextOffset);
 	}
 
 	/**
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr17.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr17.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr17.java	(working copy)
@@ -64,7 +64,7 @@
 			len <<= 1;
 			count++;
 		}
-		putN(writer, count, len);
+		writer.putN(count, len);
 
 		// Calculate the header. This code is unlikely to survive the finding of another example!
 		// Have no idea what the real thinking behind this is.
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr18.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr18.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr18.java	(working copy)
@@ -35,7 +35,7 @@
 		int poiSize = getSizes().getSize(19);
 		for (Mdr18Record pt : poiTypes) {
 			writer.putChar((char) (pt.getType() | 0x4000));
-			putN(writer, poiSize, pt.getRecord());
+			writer.putN(poiSize, pt.getRecord());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr19.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr19.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr19.java	(working copy)
@@ -69,7 +69,7 @@
 				index |= flag;
 				lastName = name;
 			}
-			putN(writer, n, index);
+			writer.putN(n, index);
 
 			int type = MdrUtils.fullTypeToNaturalType(p.getType());
 			if (type != lastType) {
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr1MapIndex.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr1MapIndex.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr1MapIndex.java	(working copy)
@@ -44,22 +44,7 @@
 	}
 
 	public void addPointer(int recordNumber) {
-		switch (pointerSize) {
-		case 4:
-			subWriter.putInt(recordNumber);
-			break;
-		case 3:
-			subWriter.put3(recordNumber);
-			break;
-		case 2:
-			subWriter.putChar((char) recordNumber);
-			break;
-		case 1:
-			subWriter.put((byte) recordNumber);
-			break;
-		default:
-			assert false;
-		}
+		subWriter.putN(pointerSize, recordNumber);
 	}
 	
 	private int sectionToSubsection(int n) {
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr25.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr25.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr25.java	(working copy)
@@ -81,7 +81,7 @@
 	public void writeSectData(ImgFileWriter writer) {
 		int size = getItemSize();
 		for (Mdr5Record city : cities) {
-			putN(writer, size,  city.getGlobalCityIndex());
+			writer.putN(size,  city.getGlobalCityIndex());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr26.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr26.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr26.java	(working copy)
@@ -74,7 +74,7 @@
 	public void writeSectData(ImgFileWriter writer) {
 		int size = getSizes().getSize(28);
 		for (Mdr28Record record : index) {
-			putN(writer, size, record.getIndex());
+			writer.putN(size, record.getIndex());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr27.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr27.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr27.java	(working copy)
@@ -76,7 +76,7 @@
 	public void writeSectData(ImgFileWriter writer) {
 		int size = getItemSize();
 		for (Mdr5Record city : cities) {
-			putN(writer, size,  city.getGlobalCityIndex());
+			writer.putN(size,  city.getGlobalCityIndex());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr28.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr28.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr28.java	(working copy)
@@ -73,10 +73,10 @@
 		int size27 = sizes.getSize(27);
 
 		for (Mdr28Record mdr28 : index) {
-			putN(writer, size23, mdr28.getMdr23());
+			writer.putN(size23, mdr28.getMdr23());
 			putStringOffset(writer, mdr28.getStrOffset());
-			putN(writer, size21, mdr28.getMdr21());
-			putN(writer, size27, mdr28.getMdr27());
+			writer.putN(size21, mdr28.getMdr21());
+			writer.putN(size27, mdr28.getMdr27());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr29.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr29.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr29.java	(working copy)
@@ -15,6 +15,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 import uk.me.parabola.imgfmt.app.srt.Sort;
 import uk.me.parabola.imgfmt.app.srt.SortKey;
@@ -82,17 +83,17 @@
 		int size22 = sizes.getSize(22);
 		int size25 = sizes.getSize(5);  // NB appears to be size of 5 (cities), not 25 (cities with country).
 		int size26 = has26? sizes.getSize(26): 0;
-		int size17 = numberToPointerSize(max17);
+		int size17 = Utils.numberToPointerSize(max17);
 		for (Mdr29Record record : index) {
-			putN(writer, size24, record.getMdr24());
+			writer.putN(size24, record.getMdr24());
 			if (hasString)
 				putStringOffset(writer, record.getStrOffset());
-			putN(writer, size22, record.getMdr22());
-			putN(writer, size25, record.getMdr25());
+			writer.putN(size22, record.getMdr22());
+			writer.putN(size25, record.getMdr25());
 			if (has26)
-				putN(writer, size26, record.getMdr26());
+				writer.putN(size26, record.getMdr26());
 			if (has17)
-				putN(writer, size17, record.getMdr17());
+				writer.putN(size17, record.getMdr17());
 		}
 	}
 
@@ -111,7 +112,7 @@
 				;
 		if (isForDevice()) {
 			if (!getConfig().getSort().isMulti())
-				size += numberToPointerSize(max17);
+				size += Utils.numberToPointerSize(max17);
 		} else {
 			size += sizes.getStrOffSize();
 			size += sizes.getSize(26);
@@ -138,7 +139,7 @@
 		if (isForDevice()) {
 			int magic = 0x6; // 22 and 25
 			if (!getConfig().getSort().isMulti())
-				magic |= numberToPointerSize(max17) << 4;
+				magic |= Utils.numberToPointerSize(max17) << 4;
 			return magic; // +17, -26, -strings
 		}
 		else
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr2x.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr2x.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr2x.java	(working copy)
@@ -70,7 +70,7 @@
 					int trailingFlags = ((rr & 1) == 0) ? 1 : 0;
 					// trailingFlags |= s.getB() << 1;
 					// trailingFlags |= s.getS() << (1 + partialBShift);
-					putN(writer, partialInfoSize, trailingFlags);
+					writer.putN(partialInfoSize, trailingFlags);
 				}
 			} else {
 				int rr = street.checkFullRepeat(prev, collator);
@@ -78,7 +78,7 @@
 					repeat = 0;
 
 				int index = street.getIndex();
-				putN(writer, size, (index << 1) | repeat);
+				writer.putN(size, (index << 1) | repeat);
 			}
 
 			prev = street;
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr5.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr5.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr5.java	(working copy)
@@ -17,6 +17,7 @@
 import java.util.Collections;
 import java.util.List;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 import uk.me.parabola.imgfmt.app.srt.MultiSortKey;
 import uk.me.parabola.imgfmt.app.srt.Sort;
@@ -51,7 +52,7 @@
 	 * Called after all cities to sort and number them.
 	 */
 	public void preWriteImpl() {
-		localCitySize = numberToPointerSize(maxCityIndex + 1);
+		localCitySize = Utils.numberToPointerSize(maxCityIndex + 1);
 
 		List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
 		Sort sort = getConfig().getSort();
@@ -216,7 +217,7 @@
 				writer.putChar((char) region);
 			if (hasString)
 				putStringOffset(writer, city.getStringOffset());
-			putN(writer, size20, city.getMdr20());
+			writer.putN(size20, city.getMdr20());
 		}
 	}
 
@@ -225,10 +226,7 @@
 	 * and not the global city index used in mdr11.
 	 */
 	private void putLocalCityIndex(ImgFileWriter writer, int cityIndex) {
-		if (localCitySize == 2) // 3 probably not possible in actual maps.
-			writer.putChar((char) cityIndex);
-		else
-			writer.put((byte) cityIndex);
+		writer.putN(localCitySize, cityIndex);
 	}
 
 	/**
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr6.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr6.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr6.java	(working copy)
@@ -56,7 +56,7 @@
 			addIndexPointer(z.getMapIndex(), record++);
 
 			putMapIndex(writer, z.getMapIndex());
-			putN(writer, zipSize, z.getZipIndex());
+			writer.putN(zipSize, z.getZipIndex());
 			if (hasString)
 				putStringOffset(writer, z.getStringOffset());
 		}
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr7.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr7.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr7.java	(working copy)
@@ -367,7 +367,7 @@
 				int trailingFlags = ((rr & 1) == 0) ? 1 : 0;
 				// trailingFlags |= s.getB() << 1;
 				// trailingFlags |= s.getS() << (1 + partialBShift);
-				putN(writer, partialInfoSize, trailingFlags);
+				writer.putN(partialInfoSize, trailingFlags);
 			}
 			last = s;
 		}
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr8.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr8.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr8.java	(working copy)
@@ -44,7 +44,7 @@
 			for (int i = 0; i< STRING_WIDTH; i++) {
 				writer.put((byte) s.getPrefix()[i]);
 			}
-			putN(writer, size, s.getRecordNumber());
+			writer.putN(size, s.getRecordNumber());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/Mdr9.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/Mdr9.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/Mdr9.java	(working copy)
@@ -36,7 +36,7 @@
 		for (Map.Entry<Integer, Integer> ent : index.entrySet()) {
 			int group = ent.getKey();
 			writer.put((byte) group);
-			putN(writer, poiSize, ent.getValue());
+			writer.putN(poiSize, ent.getValue());
 		}
 	}
 
Index: src/uk/me/parabola/imgfmt/app/mdr/MdrMapSection.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/MdrMapSection.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/MdrMapSection.java	(working copy)
@@ -12,6 +12,7 @@
  */
 package uk.me.parabola.imgfmt.app.mdr;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 
 /**
@@ -37,7 +38,7 @@
 		// There are no flags or minimums required here, unlike in setPointerSize()
 		// which does a similar thing.
 		int n = getNumberOfItems();
-		index.setPointerSize(sectionNumber, numberToPointerSize(n));
+		index.setPointerSize(sectionNumber, Utils.numberToPointerSize(n));
 	}
 
 	/**
@@ -52,18 +53,18 @@
 
 	protected void putCityIndex(ImgFileWriter writer, int cityIndex, boolean isNew) {
 		int flag = (isNew && cityIndex > 0)? getSizes().getCityFlag(): 0;
-		putN(writer, getSizes().getCitySizeFlagged(), cityIndex | flag);
+		writer.putN(getSizes().getCitySizeFlagged(), cityIndex | flag);
 	}
 
 	protected void putRegionIndex(ImgFileWriter writer, int region) {
 		// This is only called when putCityIndex might also be called and so has to be
 		// the same size (probably ;)
-		putN(writer, getSizes().getCitySizeFlagged(), region);
+		writer.putN(getSizes().getCitySizeFlagged(), region);
 	}
 
 	protected void putPoiIndex(ImgFileWriter writer, int poiIndex, boolean isNew) {
 		int flag = isNew? getSizes().getPoiFlag(): 0;
-		putN(writer, getSizes().getPoiSizeFlagged(), poiIndex | flag);
+		writer.putN(getSizes().getPoiSizeFlagged(), poiIndex | flag);
 	}
 
 	protected boolean hasFlag(int val) {
Index: src/uk/me/parabola/imgfmt/app/mdr/MdrSection.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/MdrSection.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/MdrSection.java	(working copy)
@@ -12,6 +12,7 @@
  */
 package uk.me.parabola.imgfmt.app.mdr;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 
 /**
@@ -48,44 +49,13 @@
 	}
 
 	protected void putMapIndex(ImgFileWriter writer, int mapIndex) {
-		putN(writer, sizes.getMapSize(), mapIndex);
+		writer.putN(sizes.getMapSize(), mapIndex);
 	}
 
 	protected void putStringOffset(ImgFileWriter writer, int strOff) {
-		putN(writer, sizes.getStrOffSize(), strOff);
+		writer.putN(sizes.getStrOffSize(), strOff);
 	}
 
-	protected void putN(ImgFileWriter writer, int n, int value) {
-		switch (n) {
-		case 1:
-			writer.put((byte) value);
-			break;
-		case 2:
-			writer.putChar((char) value);
-			break;
-		case 3:
-			writer.put3(value);
-			break;
-		case 4:
-			writer.putInt(value);
-			break;
-		default: // Don't write anything.
-			assert false;
-			break;
-		}
-	}
-
-	protected static int numberToPointerSize(int n) {
-		if (n > 0xffffff)
-			return 4;
-		else if (n > 0xffff)
-			return 3;
-		else if (n > 0xff)
-			return 2;
-		else
-			return 1;
-	}
-
 	/**
 	 * The number of records in this section.
 	 * @return The number of items in the section.
@@ -113,7 +83,7 @@
 	 * to store the largest record number in this section.
 	 */
 	public int getSizeForRecord() {
-		return numberToPointerSize(getNumberOfItems());
+		return Utils.numberToPointerSize(getNumberOfItems());
 	}
 
 	/**
@@ -178,7 +148,7 @@
 		 * one bit flag.
 		 */
 		public int getCitySizeFlagged() {
-			return Math.max(2, numberToPointerSize(sections[5].getNumberOfItems() << 1));
+			return Math.max(2, Utils.numberToPointerSize(sections[5].getNumberOfItems() << 1));
 		}
 
 		public int getCityFlag() {
@@ -190,7 +160,7 @@
 		}
 
 		public int getStreetSizeFlagged() {
-			return numberToPointerSize(sections[7].getNumberOfItems() << 1);
+			return Utils.numberToPointerSize(sections[7].getNumberOfItems() << 1);
 		}
 
 		public int getPoiSize() {
@@ -207,7 +177,7 @@
 		 * and a flag bit.
 		 */
 		public int getPoiSizeFlagged() {
-			return numberToPointerSize(sections[11].getNumberOfItems() << 1);
+			return Utils.numberToPointerSize(sections[11].getNumberOfItems() << 1);
 		}
 
 		public int getPoiFlag() {
Index: src/uk/me/parabola/imgfmt/app/mdr/PrefixIndex.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/mdr/PrefixIndex.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/mdr/PrefixIndex.java	(working copy)
@@ -16,6 +16,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 import uk.me.parabola.imgfmt.app.srt.Sort;
 import uk.me.parabola.imgfmt.app.srt.Sort.SrtCollator;
@@ -114,17 +115,17 @@
 	 * Write the section or subsection.
 	 */
 	public void writeSectData(ImgFileWriter writer) {
-		int size = numberToPointerSize(maxIndex);
+		int size = Utils.numberToPointerSize(maxIndex);
 		for (Mdr8Record s : index) {
 			for (int i = 0; i< prefixLength; i++) {
 				writer.put((byte) s.getPrefix()[i]);
 			}
-			putN(writer, size, s.getRecordNumber());
+			writer.putN(size, s.getRecordNumber());
 		}
 	}
 
 	public int getItemSize() {
-		return prefixLength + numberToPointerSize(maxIndex);
+		return prefixLength + Utils.numberToPointerSize(maxIndex);
 	}
 
 	protected int numberOfItems() {
Index: src/uk/me/parabola/imgfmt/app/net/NETFileReader.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/net/NETFileReader.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/net/NETFileReader.java	(working copy)
@@ -22,6 +22,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
 import uk.me.parabola.imgfmt.app.ImgFile;
 import uk.me.parabola.imgfmt.app.ImgFileReader;
@@ -152,7 +153,7 @@
 		indexes.clear();
 		if (flag == 2) {
 			// fetch city/zip index
-			int ind = (size == 2)? reader.getChar(): (reader.get() & 0xff);
+			int ind = reader.getUint(size);
 			if (ind != 0)
 				indexes.add(ind-1);
 		} else if (flag == 3) {
@@ -209,15 +210,7 @@
 			assert false : "ERRROR overflow";
 			return 0;
 		}
-		int cnum;
-		if (size == 1)
-			cnum = reader.get() & 0xff;
-		else if (size == 2)
-			cnum = reader.getChar();
-		else {
-			assert false : "unexpected size value" + size;
-			return 0;
-		}
+		int cnum = reader.getUint(size);
 		return cnum;
 	}
 
@@ -300,12 +293,12 @@
 
 	public void setCities(List<City> cities) {
 		this.cities = cities;
-		this.citySize = cities.size() > 255? 2: 1;
+		this.citySize = Utils.numberToPointerSize(cities.size());
 	}
 
 	public void setZips(List<Zip> zips) {
 		this.zips = zips;
-		this.zipSize = zips.size() > 255? 2: 1;
+		this.zipSize = Utils.numberToPointerSize(zips.size());
 	}
 
 	public void setLabels(LBLFileReader labels) {
Index: src/uk/me/parabola/imgfmt/app/net/NumberPreparer.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/net/NumberPreparer.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/net/NumberPreparer.java	(working copy)
@@ -17,6 +17,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.BitWriter;
 import uk.me.parabola.imgfmt.app.lbl.City;
 import uk.me.parabola.imgfmt.app.lbl.Zip;
@@ -998,12 +999,14 @@
 	void writeIndex(int val){
 		if (val <= 0)
 			return;
-		if (numItems > 255){
-			buf.write((byte) val & 0xff);
-			buf.write((byte) (val >> 8));
-		}
-		else 
-			buf.write((byte) val);
+		int ptrSize = Utils.numberToPointerSize(numItems);
+		buf.write(val);
+		if (ptrSize <= 1)
+			return;
+		buf.write(val >> 8);
+		if (ptrSize <= 2)
+			return;
+		buf.write(val >> 16);
 	}
 
 }
Index: src/uk/me/parabola/imgfmt/app/net/RoadDef.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/net/RoadDef.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/net/RoadDef.java	(working copy)
@@ -27,6 +27,7 @@
 import java.util.TreeMap;
 
 import uk.me.parabola.imgfmt.MapFailedException;
+import uk.me.parabola.imgfmt.Utils;
 import uk.me.parabola.imgfmt.app.BitWriter;
 import uk.me.parabola.imgfmt.app.ImgFileWriter;
 import uk.me.parabola.imgfmt.app.Label;
@@ -243,7 +244,7 @@
 			len = (numbers == null)  ? 0: numbers.zipWriter.getBuffer().size();
 			if (len > 0){
 				zipBuf = numbers.zipWriter.getBuffer();
-				flag = (len > 255) ? 1 : 0;
+				flag = Utils.numberToPointerSize(len) - 1;
 			} else 
 				flag = (zip == null) ? 3 : 2;
 			code |= flag << 2;
@@ -251,7 +252,7 @@
 			len = (numbers == null)  ? 0: numbers.cityWriter.getBuffer().size();
 			if (len > 0){
 				cityBuf = numbers.cityWriter.getBuffer();
-				flag = (len > 255) ? 1 : 0;
+				flag = Utils.numberToPointerSize(len) - 1;
 			} else 
 				flag = (city == null) ? 3 : 2;
 			code |= flag << 4;
@@ -258,7 +259,7 @@
 			
 			len = (numbers == null) ? 0 : numbers.fetchBitStream().getLength();
 			if (len > 0){
-				flag = (len > 255) ? 1 : 0;
+				flag = Utils.numberToPointerSize(len) - 1;
 			} else 
 				flag = 3;
 			code |= flag << 6;
@@ -268,42 +269,27 @@
 			
 			if (zipBuf != null){
 				len = zipBuf.size();
-				if (len > 255)
-					writer.putChar((char) len);
-				else
-					writer.put((byte) len);
+				writer.putN(Utils.numberToPointerSize(len), len);
 				writer.put(zipBuf.toByteArray());
 			} else {
 				if(zip != null) {
 					char zipIndex = (char)zip.getIndex();
-					if(numZips > 255)
-						writer.putChar(zipIndex);
-					else
-						writer.put((byte)zipIndex);
+					writer.putN(Utils.numberToPointerSize(numZips), zipIndex);
 				}
 			}
 			if (cityBuf != null){
 				len = cityBuf.size();
-				if (len > 255)
-					writer.putChar((char) len);
-				else
-					writer.put((byte) len);
+				writer.putN(Utils.numberToPointerSize(len), len);
 				writer.put(cityBuf.toByteArray());
 			} else {
 				if(city != null) {
 					char cityIndex = (char)city.getIndex();
-					if(numCities > 255)
-						writer.putChar(cityIndex);
-					else
-						writer.put((byte)cityIndex);
+					writer.putN(Utils.numberToPointerSize(numCities), cityIndex);
 				}
 			}
 			if (numbers != null) {
 				BitWriter bw = numbers.fetchBitStream();
-				if (bw.getLength() > 255)
-					writer.putChar((char) bw.getLength());
-				else
-					writer.put((byte) bw.getLength());
+				writer.putN(Utils.numberToPointerSize(bw.getLength()), bw.getLength());
 				writer.put(bw.getBytes(), 0, bw.getLength());
 			}
 		}
Index: src/uk/me/parabola/imgfmt/app/typ/TYPFile.java
===================================================================
--- src/uk/me/parabola/imgfmt/app/typ/TYPFile.java	(revision 3976)
+++ src/uk/me/parabola/imgfmt/app/typ/TYPFile.java	(working copy)
@@ -135,12 +135,12 @@
 
 	private void writeStrIndex(ImgFileWriter in) {
 		SectionWriter writer = header.getStringIndex().makeSectionWriter(in);
-		int psize = ptrSize(header.getLabels().getSize());
+		int psize = Utils.numberToPointerSize(header.getLabels().getSize());
 		header.getStringIndex().setItemSize((char) (3 + psize));
 
 		for (Map.Entry<Integer, Integer> ent : strToType.entrySet()) {
-			putN(writer, psize, ent.getKey());
-			putN(writer, 3, ent.getValue());
+			writer.putN(psize, ent.getKey());
+			writer.put3(ent.getValue());
 		}
 		Utils.closeFile(writer);
 	}
@@ -147,12 +147,12 @@
 
 	private void writerTypeIndex(ImgFileWriter in) {
 		SectionWriter writer = header.getTypeIndex().makeSectionWriter(in);
-		int psize = ptrSize(header.getLabels().getSize());
+		int psize = Utils.numberToPointerSize(header.getLabels().getSize());
 		header.getTypeIndex().setItemSize((char) (3 + psize));
 
 		for (Map.Entry<Integer, Integer> ent : typeToStr.entrySet()) {
-			putN(writer, 3, ent.getKey());
-			putN(writer, psize, ent.getValue());
+			writer.put3(ent.getKey());
+			writer.putN(psize, ent.getValue());
 		}
 		Utils.closeFile(writer);
 	}
@@ -170,7 +170,7 @@
 
 		int size = dataSection.getSize();
 		int typeSize = indexSection.getItemSize();
-		int psize = ptrSize(size);
+		int psize = Utils.numberToPointerSize(size);
 
 		indexSection.setItemSize((char) (typeSize + psize));
 
@@ -178,8 +178,8 @@
 		for (TypElement elem : elementData) {
 			int offset = elem.getOffset();
 			int type = elem.getTypeForFile();
-			putN(subWriter, typeSize, type);
-			putN(subWriter, psize, offset);
+			subWriter.putN(typeSize, type);
+			subWriter.putN(psize, offset);
 		}
 		Utils.closeFile(subWriter);
 
@@ -195,37 +195,6 @@
 		}
 	}
 
-	private int ptrSize(int size) {
-		int psize = 1;
-		if (size > 0xffffff)
-			psize = 4;
-		else if (size > 0xffff)
-			psize = 3;
-		else if (size > 0xff)
-			psize = 2;
-		return psize;
-	}
-
-	protected void putN(ImgFileWriter writer, int n, int value) {
-		switch (n) {
-		case 1:
-			writer.put((byte) value);
-			break;
-		case 2:
-			writer.putChar((char) value);
-			break;
-		case 3:
-			writer.put3(value);
-			break;
-		case 4:
-			writer.putInt(value);
-			break;
-		default: // Don't write anything.
-			assert false;
-			break;
-		}
-	}
-	
 	public void setData(TypData data) {
 		this.data = data;
 		TypParam param = data.getParam();
Index: src/uk/me/parabola/mkgmap/build/MapBuilder.java
===================================================================
--- src/uk/me/parabola/mkgmap/build/MapBuilder.java	(revision 3976)
+++ src/uk/me/parabola/mkgmap/build/MapBuilder.java	(working copy)
@@ -267,8 +267,26 @@
 			}
 			netFile.writePost(rgnFile.getWriter());
 		}
+		warnAbout3ByteImgRefs();
 	}
-	
+
+	private void warnAbout3ByteImgRefs() {
+		String infoMsg = "- more than 65535 might cause indexing problems and excess size. Suggest splitter with lower --max-nodes";
+		int itemCount;
+		itemCount = lblFile.numCities();
+		if (itemCount > 0xffff)
+			log.error("Map contains", itemCount, "Cities", infoMsg);
+		itemCount = lblFile.numZips();
+		if (itemCount > 0xffff)
+			log.error("Map contains", itemCount, "Zips", infoMsg);
+		itemCount = lblFile.numHighways();
+		if (itemCount > 0xffff)
+			log.error("Map contains", itemCount, "Highways", infoMsg);
+		itemCount = lblFile.numExitFacilities();
+		if (itemCount > 0xffff)
+			log.error("Map contains", itemCount, "Exit facilities", infoMsg);
+	} // warnAbout3ByteImgRefs
+
 	private Country getDefaultCountry() {
 		if (defaultCountry == null && lblFile != null) {
 			defaultCountry = lblFile.createCountry(countryName, countryAbbr);
