added some logic to the code