Optimize if then statement