Refactored the code a bit to use promises with .then() instead of success/error calls.