removed perl -an and implemented explicit looping