Fix invalid index errors
Fix pass by reference issue