Simplified scanDir function and attempted to get rid of a memory leak