replaced some code with functions and introduced 2 new functions base on TUSHKAS work