Fixed a lot of minor bugs related to readdir