optimize opendir code to be smaller