Refactor and fixed test cases error