Fix panic

Added range checks.