Adding error checking and ajax result retrival