removed duplicate code/added a common function