Bug fixes on infinite loop