Yash's working cycle detection code