X-Requested-With added and better error handling