Fixed minor errors in loops