added some additional logic. not entirely working.