cleanup and fix tem var bug