finally got all logic working