corrected the algorithm. still a little off and does not include exception.