Updated logic according to code review.