Code clean up, restructuring and bug fixes.