Heavy refactoring.

Moved the shared memory and shared structures initialization to the constructor, so it's called only once.

It is still subject to more refactoring and bug fixing.