Fixed nested loops
Added explicit conversion