starting to understand js flow but code not DRY