(1) Refernce count fixes
(2) Minor refactoring
(3) More error processing