Removed transactional inserts and updates from the loop. Instead all inserts are on table variables
Added some comments on all return statements