Rewrite to delay execution of code add sed and use case instead of if