used substr instead of charAt for older browsers