Extract duplication
Removing some duplication
Add documentation