more work for bug 843004 part 3. proper code structure and bug fixes