added condition checks to decrease panic