Refactored energy.js