Fix variable scopes and include some javadoc.