reference counting for tables
code cleanup and more reference counting for tables