Added condition checks for adding gates