refactored some duplicating code