Optimizing code, memory usage. Added some documentation on comments. Bugfixes.