Refactoring and add PHPUnit testing functionality