Major refactor of the password generation

Found some bugs, did not find any clean solutions... had to resort to
simply refactoring it to a substantial extent.