Reorganized code and changed ajax method