Refactoring and examples

merging refactoring branch into master.