Added Terminating Condition and Flag Parsing