Refactored example code and tested