Code refactoring.
Added validation (TO DO: document it)