improvements and add unit test