Added tests and try to fix code duplication