refactor jerry's contribution, add tests & doc