fix algoritm and implement delay on loop