Final functions implemntation. Fully tested.