improved logic,restructured,indentation