code cleaned bug flank detection corrected