Made sure both implementations match as closely as possible. Ported over the comments from C# to Java. Cleaned up code layout.