Improve with function and add unit tests