[TASK] Refactor code and refine tests