fixed recursive function.  May need fine tuning a little.