Working function
Added comments