code cleaning and add unit tests