Fix encoding issues and endless recursion

This fixes #421 and #422.

While preparing this PR I also found that `randomize` was using hardcoded `/` as path separators so I made sure the code only uses `os.path.join` to build the randomized path.