more robust implementation for listFiles