Refactor shared logic in folders functions