fix wrong order or loop condition