refactored with collapse function