refactoring code, adding phpunit tests