Reorganised code and corrected ajax request