Not including BASE_DIR in an opendir call can fail. Also test that opendir does not return false to protect from infinite loops