[TASK] Add unit test and do some refactoring