Reduced AJAX calls to server. Fixed undefined variable issue.