Refactor randWords function