[misc] Refactoring some code, added more unit tests
[misc] Added missing tests