made detection code better