Improved place-detection code