Cleaned up recursion routine a bit