Split it into multiple functions and make it more readable using async.waterfal.