ajax request was finishing after the rest of the code had run. Added a setTimeout into the mix