Moved functions outside ajax request