Updated Java implementation.