Minor additional check
Added index range checking
Added function docstrings