refactored code. moved common method to helpers