optimising recurtion