Refactor flatdir to make it easier to extend