Fixed bugs after latest merge. Code cleanup.