Optimized for memory usage, Added a bunch of tests.