C# port of MapGen2 from Amit Patel