Re-factored expanding and collapsing of elements.