add unit tests and clean up codes