removed duplicate code and added an if