Fixed substr to use string lengths instead of magic numbers