Refactor and add documentation to example