moved detection of cycles