Changed the code not using flag