Implemented more memory efficient approach