A rewrite of the original C# version that uses .net 4.0 features