Update Main.cs

Many bug fixes.