This 3 functions are totally taken from internet.