Re-factored code to be simpler

This code is getting ready to be run a-syncronously.