Replaced collapse code with method from nickmass