Code reuse with a function and some fix