realized that algorithm can scale as O(N) rather than O(N^2) so appropriate changes were made