First version of example 5. Also fixed some bugs.