Efficient code with function