Some code rearrangement and minor changes.