Fully working function
Includes other functions