code optimized with AJAX calls