Significantly refactor code to leverage JS callback organization. Fixes code for images