flags added for input, output function added, code modularized