correct substring range calculation