Updated script to use encode