Code improvement for ajax request handler