Some refactoring and improvement to password generation algorithm