Small refactoring
Added support for clustering.
improved memory management.