using splice to trim instead of 'while' loop