Made algorthm (hopefully) faster