magic index recursive and optimized