start of detection algorithm