Tertiary functions added and some code cleaning