Fix two loop bugs which makes CPU 100%