refactored to make handling of ajax request more clear