clean up the code and added unittest rather than using cogent