added when clauses when appropriate