Fixed bugs.
Address some PR comments and fixed other issues.