Refactored episode code