performance improvement of the internal loop