a little abstaction
improve the logic to reduce memory consumption
improving comment
changing to core php functions instead of Varien classes to reduce memory consumption
formatting fix