Optimize duplicated code