Refactoring (taking notes from PEP8). Now works correctly on directories with a trailing /.