Refactoring + add PHPUnit tests