added table functions
code cleanup, removed log statements
wrapped js into immediate function