Tidy up code and recursive search throws error