improved the flow of the logic