Moved expand code into the element functions, added before and after functions.