improved running time and added argument to set max distance for planets.