Bug fixed part of the collapse algorithm