refactor hairy function