Huge refactor. Handle asynchronous calls better with jQuery.when