Fixed a bug in the C# version and begun porting to Java.