added some logic. need to refactor some of the code