function that calculates factorial