Fixed bugs and code cleanup.