added max iteration limit