public void write(File fileName)
	{
		FileWrite fp = new FileWrite(fileName);

		if (fp != null)
		{
			StringBuilder levelContent = new StringBuilder();

			levelContent.append("# File autogenerated with Editor. Do not modify.\n\n");
			levelContent.append("COLLISION default.col 256\n\n"); // temp

			// In this algorithm I make an assumption that sequentially accessing elements in an array
			// is faster than calling getTile() method of an ArrayList object to retreive the elements.
			for (int i = layers.size(); i > 0; i--)
			{
				LevelLayer curElem = layers.get(i - 1);
				String imgPath = curElem.getImgPath();
				int levelWidth = curElem.getWidth();
				int levelHeight = curElem.getHeight();

				ArrayList<ArrayList<Integer>> list = curElem.getList();
				Integer [][] array = new Integer[list.size()][];

				for (int n = 0; n < list.size(); n++)
				{
					array[n] = list.get(n).toArray(new Integer[0]);
				}

				System.out.printf("Converted\n");

				levelContent.append("LAYER " + curElem.getId() + "\n");
				levelContent.append("IMG " + tilesetName + "0.bmp " + curElem.getTileW() + " " + curElem.getTileH() + " " + curElem.getImgRowW() + " " + curElem.getImgSize() + "\n");

				for (int y = 0; y < levelHeight; y++)
				{
					for (int x = 0; x < levelWidth; x++)
					{
						levelContent.append(Integer.toString(array[y][x]));

						if (x < levelWidth - 1)
						{
							levelContent.append("\t");
						}
					}
					levelContent.append("\n");
				}

				// Old method
//				for (int y = 0; y < levelHeight; y++)
//				{
//					for (int x = 0; x < levelWidth; x++)
//					{
//						levelContent.append(curElem.getTile(x, y)); // this is resource wastful

//						if (x < levelWidth - 1)
//						{
//							levelContent.append("\t");
//						}
//					}

//					levelContent.append("\n";
//				}

				levelContent.append("END\n\n");
			}

			levelContent.append("OBJECTS\n");
			for (GameObject curObj : this.objects)
			{
				levelContent.append(curObj.getName() + "\t" + curObj.getX() + "\t" + curObj.getY() + "\t" + (curObj.getDirection() ? 1 : 0) + "\n");
			}
//			levelContent.append("player\t32\t48\t1\n"); // temp
			levelContent.append("END\n");

			System.out.printf("Writing to file\n");
			fp.writeLine(levelContent.toString());

			String tmpName = fileName.getName();
			if (!tmpName.equals("lvl.tmp"))
			{
				this.levelName = tmpName;
				this.setModified(false);
			}
		}

		fp.close();
	}