Used await properly so things don't appear to be sent out of sync. Added comments and cleanup.