added possibility to break infinite loop when using max deviation