Created `encode()` to keep the code DRY, refactored `cache()` to accept a callback function