bugs and loops fix