Refactoring of readdir().

This is a clean, but untested fix for the rewinddir() bug.