fixed bug in detection loop + formatting