cleaned up collapse() and added some comments.