remove the redundant code and make a common method