Code clean-up. More memory-efficient implementation.