Added termination conditions

also handling when f*N == 0