Completed working function - without comments