Fix unit ref. error and replace some code with ternary operator