How to speed up the "if then else"