refactors to exclude unshift method and using square root to finish loop faster