slower but smaller code using search