Updated code based on @tomMalbran's feedback.