Refactor endowments.js
Refactor JS to make it DRY