Refactored unitest and fixed a bug