refactor likelihood function