Improve Collapse sample code to be more reusable