Changed algorithm to speed up selection process