optimised: merge 2 functions into 1