overhauled and simplified bed file collapsing