moved some conditions into for-loop