generalizing collapsible code for use elsewhere