Added mapWithIndex function and fixed bugs